
import java.io.ByteArrayOutputStream;import java.io.PrintStream;
import static org.junit.Assert.*;
public class Game
{
Game game = null;
Result result = null;
PrintStream console = null;
static ByteArrayOutputStream bytes = null;
String lineSeparator = System.getProperty("line.separator");
public Result playGame(String s){
String str="";
Result flag=Result.GAMING;//用來表示游戲狀態 是X勝, Y勝,無勝,和游戲沒結束。開始默認為進行中。
String[] sArray = s.split(","); //打散字符,形式為A1,B1,B2,B3,C3的陣列
int k=1; //中間變數
int data[] = new int[9];//通過 9個元素的陣列保存棋盤的狀態
//存1為X的位置,存2是O的位置,存零是沒有占位置。
for(int j=0;j<sArray.length;j++){
if (j%2==0) k=1; else k=2;
//sArray[j].charAt(0)-'A' 提取"A1"這個字串中的第一個字符與'A'相減,取得所在列的次序
//sArray[j].charAt(1)-'0') 提取"A1"這個字串中的第二個字符與'0'相減,取得所在行的次序
// 列數*3+行數-1 得到在陣列中位置
data[(sArray[j].charAt(1)-'0'-1)*3+(sArray[j].charAt(0)-'A')]=k;
str+=" A B C"+lineSeparator;
for(int i=0;i<9;i++){
if(i==0) str+="1";
if(i==3) str+="2";
if(i==6) str+="3";
if(data[i]==0) str+=" _";
if(data[i]==1) str+=" X";
if(data[i]==2) str+=" O";
if(i==2||i==5||i==8) str+=lineSeparator;
}
}
for (k=0;k<9 ;k++ ) if (data[k]==0) break;
if (k==9) flag=Result.DRAW;//如果陣列元素中沒有0代表沒有位置了。
//分析X勝的情況 a=1的情況
int a=1;
if (data[0]== a && data[1] == a && data[2] == a) flag=Result.X_WIN;
if (data[3] == a && data[4] == a && data[5] == a) flag=Result.X_WIN;
if (data[6] == a && data[7] == a && data[8] == a) flag=Result.X_WIN;
if (data[0] == a && data[3] == a && data[6] == a) flag=Result.X_WIN;
if (data[1] == a && data[4] == a && data[7] == a) flag=Result.X_WIN;
if (data[2] == a && data[5] == a && data[8] == a) flag=Result.X_WIN;
if (data[0] == a && data[4] == a && data[8] == a) flag=Result.X_WIN;
if (data[2] == a && data[4] == a && data[6] == a) flag=Result.X_WIN;
if (flag!=Result.X_WIN) //如果X不勝,分析O是否勝
{
a=2;
if (data[0] == a && data[1] == a && data[2] == a) flag=Result.O_WIN;
if (data[3] == a && data[4] == a && data[5] == a) flag=Result.O_WIN;
if (data[6] == a && data[7] == a && data[8] == a) flag=Result.O_WIN;
if (data[0] == a && data[3] == a && data[6] == a) flag=Result.O_WIN;
if (data[1] == a && data[4] == a && data[7] == a) flag=Result.O_WIN;
if (data[2] == a && data[5] == a && data[8] == a) flag=Result.O_WIN;
if (data[0] == a && data[4] == a && data[8] == a) flag=Result.O_WIN;
if (data[2] == a && data[4] == a && data[6] == a) flag=Result.O_WIN;
}
//把str字串轉化為ByteArrayOutputStream.
bytes=new ByteArrayOutputStream();
bytes.write(str.getBytes(),0,str.getBytes().length);
return flag;//回傳 四種狀態中的一種
}
public static void main(String[] args)
{
Game game=new Game();
Result result=game.playGame("A1,B1,B2,B3,C3");
test(result);
result = game.playGame("A1,A2,B1,B2,C1");
test(result);
result = game.playGame("C1,A2,C2,B2,C3");
test(result);
result = game.playGame("C1,A2,C2,C3,B2,A3,A1,B1,B3");
test(result);
result = game.playGame("A1,C1,A2,B2,B1,A3");
test(result);
result = game.playGame("A1,A2,B1,B2,C3,C2");
test(result);
result = game.playGame("C1,A2");
test(result);
try{
game.test1();//沒有捕捉到例外,就說明是對的。
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void test(Result result){
String str= bytes.toString();
System.out.println(str);
if (result==Result.X_WIN) System.out.println("X Player WINs the game.");
if (result==Result.O_WIN) System.out.println("O Player WINs the game.");
if (result==Result.DRAW) System.out.println("No one WINs.");
if (result==Result.GAMING) System.out.println("Game is not over.");
}
@org.junit.Test
public void test1() throws Exception {
Game game=new Game();
result = game.playGame("A1,B1,B2,B3,C3");
assertEquals(Result.X_WIN,result);
assertEquals(
" A B C"+lineSeparator
+"1 X _ _"+lineSeparator
+"2 _ _ _"+lineSeparator
+"3 _ _ _"+lineSeparator
+" A B C"+lineSeparator
+"1 X O _"+lineSeparator
+"2 _ _ _"+lineSeparator
+"3 _ _ _"+lineSeparator
+" A B C"+lineSeparator
+"1 X O _"+lineSeparator
+"2 _ X _"+lineSeparator
+"3 _ _ _"+lineSeparator
+" A B C"+lineSeparator
+"1 X O _"+lineSeparator
+"2 _ X _"+lineSeparator
+"3 _ O _"+lineSeparator
+" A B C"+lineSeparator
+"1 X O _"+lineSeparator
+"2 _ X _"+lineSeparator
+"3 _ O X"+lineSeparator,bytes.toString());
}
}
public enum Result{
X_WIN,//X Player WINs the game.
O_WIN,//O Player WINs the game.
DRAW,//No one WINs.
GAMING//Game is not over.
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/83540.html
標籤:Java SE
下一篇:有人知道這是咋回事嗎?嗚嗚嗚
