目标
一个简单的, 没有过多约束的, 基于终端的,交互的四子棋游戏.
采用java视线.
核心
判断胜利的方法, 没一次落子, 搜索周围除了"上"的7个方向, 统计由近到远连续的和自己颜色相同的棋子数.
然后看对位的两个方向上之和是否为3(加上自己就是4了)
代码
//Board.java
import java.util.Scanner;
public class Board{
int width =6; int height =6;
int[] state = new int[7]; //记录落子后7个方向上同子数目
char[] board = new char[width*height];
public Board(){
for(int i=0;i<board.length;i++){
board[i] = ' ';
}
}
public static void main(String []args){
System.out.println("hi");
Board b = new Board();
b.interact();
}
public void show(){
for(int row=height-1;row>=0;row--){
for(int col=0;col<width;col++){
System.out.print(board[row*width+col]+" ");
}
System.out.print("\n");
}
for(int col = 0;col<width;col++){
System.out.print(col);
System.out.print(' ');
}
System.out.print('\n');
}
public void setRandom(){
push(0,'X');
push(0,'X');
push(0,'X');
push(0,'X');
push(0,'X');
}
public void interact(){
int res;
int col;
Scanner input = new Scanner(System.in);
while(true){
System.out.print("waiting for X column:");
col = input.nextInt();
res = push(col,'X');
if(res == 1){
show();
System.out.println("X winning!!");
break;
}
show();
System.out.print("waiting for O column:");
col = input.nextInt();
res = push(col,'O');
if(res == 1){
show();
System.out.print("O winning!!");
break;
}
show();
}
}
private int getNextIndex(int col){
int row = 0;
while(true){
char value = board[row*width+col];
if(value == ' '){
return row;
}
row++;
}
}
public int check(int row, int col, char value){
int k;
int Nrow,Ncol;
k = 1; while(true){ Nrow = row+k; Ncol = col+k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[0] = k++; }else{break;} }
k = 1; while(true){ Nrow = row+0; Ncol = col+k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[1] = k++; }else{break;} }
k = 1; while(true){ Nrow = row-k; Ncol = col+k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[2] = k++; }else{break;} }
k = 1; while(true){ Nrow = row-k; Ncol = col+0; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[3] = k++; }else{break;} }
k = 1; while(true){ Nrow = row-k; Ncol = col-k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[4] = k++; }else{break;} }
k = 1; while(true){ Nrow = row+0; Ncol = col-k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[5] = k++; }else{break;} }
k = 1; while(true){ Nrow = row+k; Ncol = col-k; if(Nrow>=0&&Nrow<width&&Ncol>=0&&Ncol<height&&board[Nrow*width+Ncol]==value){ state[6] = k++; }else{break;} }
if(state[0]+state[4]==3||state[1]+state[5]==3||state[2]+state[6]==3||state[3]==3){return 1;}
else{return 0;}
}
public int push(int col, char value){
int row=getNextIndex(col);
if(row==height){
System.out.println("full");
return -1;
}
board[row*width+col] = value;
int res = check(row,col,value);
return res;
}
public void test(){
setRandom();
System.out.print("\n");
show();
}
}
结果
hi
waiting for X column:0
X
0 1 2 3 4 5
waiting for O column:1
X O
0 1 2 3 4 5
waiting for X column:2
X O X
0 1 2 3 4 5
waiting for O column:2
O
X O X
0 1 2 3 4 5
waiting for X column:3
O
X O X X
0 1 2 3 4 5
waiting for O column:3
O O
X O X X
0 1 2 3 4 5
waiting for X column:2
X
O O
X O X X
0 1 2 3 4 5
waiting for O column:3
X O
O O
X O X X
0 1 2 3 4 5
waiting for X column:4
X O
O O
X O X X X
0 1 2 3 4 5
waiting for O column:4
X O
O O O
X O X X X
0 1 2 3 4 5
waiting for X column:4
X O X
O O O
X O X X X
0 1 2 3 4 5
waiting for O column:4
O winning!!
O
X O X
O O O
X O X X X
0 1 2 3 4 5
转载自原文链接, 如需删除请联系管理员。
原文链接:简单的四子棋游戏,转载请注明来源!