Written by Bruth_Lee in Southwest University of Science and Technology.
本游戏仅为初学者练习Java所用,不具有游戏的真实性,如果想看该正版游戏,请戳点击打开链接
import java.util.Scanner;
public class Primer_Java{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//int[] number = new int[10];
//int a;
int i,j;
final int SIZE = 3;//the size of SIZE is three
//tic-tac-toe
int[][] board = new int[SIZE][SIZE];
System.out.println("Gameplayer_1 input 1 and Gameplayer_2 input 0");
for(i=0;i<board.length;i++)//Input the element,1 representative x,0 representative o;
{
for(j=0;j<board[i].length;j++)
{
board[i][j] = in.nextInt();
}
}
boolean GotResult = false;
//Jude row column Diagonal diagonal line
int cnt = 0;
int num1 = 0;
int num2 = 0;
//Judge row and column
for(i=0;i<board.length;i++)
{
num1 = 0;
num2 = 0;
for(j=0;j<board[i].length;j++)//row
{
if(board[i][j]==1) {
num1++;
}
}
for(j=0;j<board[i].length;j++)//column
{
if(board[j][i]==1) {
num2++;
}
}
if(num1 == SIZE || num2 == SIZE)
{
System.out.println("Gameplayer_1 win!");
GotResult = true;
break;
}
if(num2 == 0 || num1 == 0)
{
System.out.println("Gameplayer_2 win!");
GotResult = true;
break;
}
}
if(GotResult == false){
num1 = 0;
num2 = 0;
//Judge diagonal diagonal line
for(i=0;i<board.length;i++)
{
if(board[i][i] == 1) {
num1++;
}
if(board[i][2-i] == 1) {
num2++;
}
}
if(num1==3 || num2 ==3) {
System.out.println("Gameplayer_1 win!");
// GotResult = true;
}
else if(num2 == 0 || num1 == 0) {
System.out.println("Gameplayer_2 win!");
// GotResult = true;
}
else {
System.out.println("Draw!");
}
}
}
}