—- code —-
import java.util.Scanner;
public class Game {
private int row;
private int col;
//private char board[][] = new char[row][col];
private char player = 'X';
public Game(int row,int col) {
this.row = row;
this.col = col;
char board[][] = new char[this.row][this.col];
}
public void inputXO() {
boolean win = true ;
Scanner inPlayer = new Scanner(System.in);
while (win) {
// player input
System.out.print("Enter : ");
int py = inPlayer.nextInt();
// choose board
if(py==1)board[0][0]=player;
else if(py==2)board[0][1]=player;
else if(py==3)board[0][2]=player;
else if(py==4)board[1][0]=player;
else if(py==5)board[1][1]=player;
else if(py==6)board[1][2]=player;
else if(py==7)board[2][0]=player;
else if(py==8)board[2][1]=player;
else if(py==9)board[2][2]=player;
}
public void main(String[] args) {
// array
System.out.println("XO Game");
display();
display();
checkwin();
if (player=='X') {
player = 'O';
}
else {
player = 'X';
}
}
}
public static void display() {
// array
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print("\t" + board[i][j]);
}
System.out.println();
}
}
public void checkwin() {
//check row
for (int i=0; i<=2;i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && (board[i][0] == 'X' || board[i][0] == 'O')) {
System.out.println("Win");
}
else if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && (board[0][i] == 'X' || board[0][i] == 'O')) {
System.out.println("Win");
}
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0] == 'X' || board[0][0] == 'O')) {
System.out.println("Win");
}
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && (board[0][2] == 'X' || board[0][2] == 'O')) {
System.out.println("Win");
}
}
}
}
ปัญหาปัจจุบัน
-ชนะแล้วสามารถเล่นต่อได้
-ตอนชนะไม่บอกชื่อฝ่ายที่ชนะ
-ตรวจสอบการชนะแนวทะแยงไม่ได้
ไม่มีความคิดเห็น:
แสดงความคิดเห็น