các bạn giúp mình hàm kiểm tra thắng trong game tictactoe được không
Mã:
public class tictactoemodel {
public enum piece{x,o,_}
private final int grid_width=3;
private final int grid_height=3;
private piece[][] boad=new piece[grid_width][grid_height];
private piece currentplayer=piece.x;
public tictactoemodel() {
reset();
}
private void reset() {
for(int i=0;i<grid_width;i++)
for(int j=0;j<grid_height;j++)
boad[i][j]=piece._;
}
public void setvalue(int row, int col,piece p)
{
boad[row][col]=p;
togetplayer();
}
private void togetplayer() {
if(currentplayer==piece.x)
currentplayer=piece.o;
else
currentplayer=piece.x;
}
public piece getcurrentplayer() {
return currentplayer;
}
public piece getvalue(int row,int col) {
return boad[row][col];
}
public piece checkwinner() {
piece winner=piece._;
//kiểm tra trên dòng
for(int i=0;i<grid_height;i++)
if(winner!=piece._&& boad[i][0]==boad[i][1] && boad[i][1]==boad[i][2])
winner=boad[i][0];
//kiểm tra trên cột
for(int i=0;i<grid_width;i++)
if(winner!=piece._&&boad[0][i]==boad[1][i]&&boad[1][i]==boad[2][i])
winner=boad[0][i];
//kiểm tra 2 đường chéo
if(winner!=piece._&&boad[0][0]==boad[1][1]&& boad[1][1]==boad[2][2])
winner=boad[0][0];
if(winner!=piece._&&boad[0][2]==boad[1][1] &&boad[1][1]==boad[2][0])
winner=boad[0][2];
return winner;
}
}
-- khi grid_height=16,grid_width=16 và số button kiểm tra là = 5
thì hàm checkwinner thay đổi như nào cho phù hợp . thanks all
Anh có biết, nỗi nhớ thỉnh thoảng không ở trong tim, mà len lách vào từng thớ thịt, trú ngụ trên làn da, đợi một bàn tay chạm đến? Em từng nghĩ yêu xa là những dòng tin nhắn dài bất tận, là những...
Trứng rung điểm G – Dịu dàng đánh thức rung cảm yêu xa