วันจันทร์ที่ 29 ตุลาคม พ.ศ. 2561

update XO [JAVA]

ทำการแก้ไข นำ การ checkwin ไปใส่ไว้ใน class Board
แก้ไข นำตัวแปร ออกจาก Main
update to bitbucket  :  https://bitbucket.org/Wanvipa/tictactoe/src/default/

===========Class Board=============

public class Board {
private int row;
private int col;
private int x = 1;
private int draw = 0;
private char board[][];
private boolean game_run;
char player = 'X';


    public Board (int row,int col) {
    this.row = row;
    this.col = col;
    this.board= new char[this.row][this.col];
     
    }
 
 
    public char getBoard(int row,int col) {
    return this.board[row][col];   
   
    }
 
    public void setBoard(int row,int col) {
    this.board[row][col] = player ;
   
    }
    public char changeplayer() {
    if (player=='X') {
    return player = 'O';
}
    else {
    return player = 'X';
}
    }
 
public void check_win(int draw){

//check lick
int check_lick = 0;
int r = 2;
System.out.println();
for(int q = 0; q < 3; q++) {
if(this.board[q][q] == 'X')check_lick += 1;
if(this.board[q][q] == 'O')check_lick -= 1;
}
lick_win(check_lick);
check_lick = 0;
for(int p = 0; p < 3; p++) {
if(this.board[p][r] == 'X')check_lick += 1;
if(this.board[p][r--] == 'O')check_lick -= 1;

}
lick_win(check_lick);

//check row and column
for(int i = 0; i < 3; i++) {
int check_row = 0; 
int check_col = 0;
for(int j = 0; j < 3; j++) {
if(this.board[i][j] == 'X')check_row += 1;
if(this.board[i][j] == 'O')check_row -= 1;
if(this.board[i][j] == 'X')check_col += 1;
if(this.board[i][j] == 'O')check_col -= 1;

}

//print result
if(check_row == 3 || check_col == 3) {
System.out.println("X is winner");
x = 0; run_game();
}else if(check_row == -3 || check_col == -3) {
System.out.println("O is winner");
x = 0; run_game();
}else if(draw == 9) {
System.out.println("Draw both");
x = 0; run_game();
}
}
draw += 1;
}
public void lick_win(int check_lick) {
if(check_lick == 3) {
System.out.println("X is winner");
x = 0; run_game();
}
if(check_lick == -3) {
System.out.println("O is winner");
x = 0; run_game();
}

}

public boolean run_game() {
if(x==0)game_run = false;
else if(x==1)game_run = true;
return game_run;
}
}

 
===========Class Main=============
public class MAIN {

    public static void main(String[]args){
    Board board = new Board(3,3);
    Display show = new Display(board);
    InputXO input = new InputXO(board);
   
    while(board.run_game()) {
    show.display();
    input.Inputboard();
    board.changeplayer();
}
        }

}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น