[PHP]/*
* 创建日期 2005-5-20
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package menglianjingGame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* @author 锋锋
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
class VertexNode {
public boolean vertex ;//顶点数据
public int[] correlativeList ;//关联顶点列表
public VertexNode()
{
}
}
class ALGraph {
public VertexNode[] AdjList ;//顶点表
public int VertexNodeCount;//顶点数
public ALGraph( int n )
{
VertexNodeCount = n;
AdjList = new VertexNode[n];
}
}
public class PlayGame {
private static ALGraph g = new ALGraph(9);
//游戏当前状态
private static boolean[] cuState = new boolean[9];
//游戏胜利状态
private static boolean[] winState = new boolean[]{true,
true,
true,
true,
false,
true,
true,
true,
true };
//游戏失败状态
private static boolean[] failState = new boolean[]{false,
false,
false,
false,
false,
false,
false,
false,
false };
public static void main(String[] args) {
initGameData();
PrintGameState(cuState);
System.out.println("游戏开始...\n 请选择\n 请输入一个0-8之间的整数!\n");
while(true){
int guess = -1;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try
{
guess = Integer.parseInt(input.readLine());
}
catch (NumberFormatException e)
{
System.out.println("请输入一个0-8之间的整数!");
continue;
}
catch (IOException e)
{
System.out.println("程序发生异常错误将被关闭!");
e.printStackTrace();
}
if ((guess > 8)||(guess == -1)||(!cuState[guess])){
System.out.println("输入无效!请重输..");
continue;
}
GameAction(guess);
UpDataGameCuState();
PrintGameState(cuState);
if(Arrays.equals(cuState,winState)){
System.out.println("恭喜!你赢了!");
break;
}
if(Arrays.equals(cuState,failState)){
System.out.println("恭喜!你输了!");
break;
}
System.out.println("游戏继续\n 请输入一个0-8之间的整数!\n");
}
}
private static void initGameData(){
System.out.println("初始化游戏...");
g.AdjList[0] = new VertexNode();
g.AdjList[0].vertex =false;
g.AdjList[0].correlativeList = new int[]{0,1,3,4};
g.AdjList[1] = new VertexNode();
g.AdjList[1].vertex =false;
g.AdjList[1].correlativeList = new int[]{0,1,2};
g.AdjList[2] = new VertexNode();
g.AdjList[2].vertex =false;
g.AdjList[2].correlativeList = new int[]{1,2,4,5};
g.AdjList[3] = new VertexNode();
g.AdjList[3].vertex =false;
g.AdjList[3].correlativeList = new int[]{0,3,6};
g.AdjList[4] = new VertexNode();
g.AdjList[4].vertex =true;
g.AdjList[4].correlativeList = new int[]{1,3,4,5,7};
g.AdjList[5] = new VertexNode();
g.AdjList[5].vertex =false;
g.AdjList[5].correlativeList = new int[]{2,5,8};
g.AdjList[6] = new VertexNode();
g.AdjList[6].vertex =false;
g.AdjList[6].correlativeList = new int[]{3,4,6,7};
g.AdjList[7] = new VertexNode();
g.AdjList[7].vertex =false;
g.AdjList[7].correlativeList = new int[]{6,7,8};
g.AdjList[8] = new VertexNode();
g.AdjList[8].vertex =false;
g.AdjList[8].correlativeList = new int[]{4,5,7,8};
UpDataGameCuState ();
System.out.println("初始化游戏完成!");
}
//根据用户输入(动作),改变游戏当前状态.
private static void GameAction(int choose){
for(int i = 0;i < g.AdjList[choose].correlativeList.length; i++){
g.AdjList[g.AdjList[choose].correlativeList].vertex =
!g.AdjList[g.AdjList[choose].correlativeList].vertex;
}
UpDataGameCuState ();//更新游戏当前状态
}
//更新游戏当前状态
private static void UpDataGameCuState (){
for(int i = 0;i < g.VertexNodeCount;i++){
cuState = g.AdjList.vertex;
}
}
//打印游戏当前状态(其中布尔值和字符转换)
private static void PrintGameState(boolean[] custate){
for( int c = 0 ;c < custate.length;c+=3)
{
for(int i =0 ;i < 3;i++){
if(custate[c+i]){
System.out.print("+ ");
}else{
System.out.print("- ");
}
}
System.out.print("\n");
}
}
}
[/PHP]
控制台下测试结果如下:
[PHP]初始化游戏...
初始化游戏完成!
- - -
- + -
- - -
游戏开始...
请选择
请输入一个0-8之间的整数!
4
- + -
+ - +
- + -
游戏继续
请输入一个0-8之间的整数!
1
+ - +
+ - +
- + -
游戏继续
请输入一个0-8之间的整数!
2
+ + -
+ + -
- + -
游戏继续
请输入一个0-8之间的整数!
3
- + -
- + -
+ + -
游戏继续
请输入一个0-8之间的整数!
4
- - -
+ - +
+ - -
游戏继续
请输入一个0-8之间的整数!
5
- - +
+ - -
+ - +
游戏继续
请输入一个0-8之间的整数!
6
- - +
- + -
- + +
游戏继续
请输入一个0-8之间的整数!
7
- - +
- + -
+ - -
游戏继续
请输入一个0-8之间的整数!
8
- - +
- - +
+ + +
游戏继续
请输入一个0-8之间的整数!
5
- - -
- - -
+ + -
游戏继续
请输入一个0-8之间的整数!
1
+ + +
- - -
+ + -
游戏继续
请输入一个0-8之间的整数!
2
+ - -
- + +
+ + -
游戏继续
请输入一个0-8之间的整数!
3
- - -
+ + +
- + -
游戏继续
请输入一个0-8之间的整数!
[/PHP]
 |