tic-tac-toe_game 井字棋游戏java源码(大一初学)
2018-02-09 12:02
393 查看
如果有更好的算法,请在下方评论学习
输入1表示 X 下的棋,输入0表示 Y 下的棋
package game;
import java.util.Scanner;
public class Game_new {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int SIZE ;// 单元格
int numOfX=0;
int numOfY=0;
System.out.println("Plase enter the game'sizes: ");
SIZE=in.nextInt();//get
int [][]game = new int [SIZE][SIZE];
boolean getResult = false;
System.out.println("Please enter your X or Y");
for(int i=0;i<game.length;i++) {
for(int k=0;k<game[i].length;k++) {
game[i][k]=in.nextInt();
}
}
for(int i=0;i<game.length;i++) {//判断主对角线
if(game[i][i]==1) {
numOfX++;
}
else {
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
}
}
if(!getResult) {//判断副对角线
numOfX=0;
numOfY=0;
for(int i=0;i<game.length;i++) {
if(game[i][SIZE-1-i]==1) {
numOfX++;
}
else {
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
}
}
}
if(!getResult) {//判断行
for(int i=0;i<game.length;i++) {
numOfX=0;
numOfY=0;
for(int k=0;k<game[i].length;k++) {
if(game[i][k]==1)
numOfX++;
else
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
break;
}
}
}
if(!getResult) {//判断列
for(int i=0;i<SIZE;i++) {
numOfX=0;
numOfY=0;
for(int k=0;k<SIZE;k++) {
if(game[k][i]==1)
numOfX++;
else
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
break;
}
}
}
if(getResult) {
if(numOfX==SIZE) {
System.out.println("X win this game!");
}
else {
System.out.println("Y win this game!");
}
}
else {
System.out.println("draw!");
}
}
}
输入1表示 X 下的棋,输入0表示 Y 下的棋
package game;
import java.util.Scanner;
public class Game_new {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int SIZE ;// 单元格
int numOfX=0;
int numOfY=0;
System.out.println("Plase enter the game'sizes: ");
SIZE=in.nextInt();//get
int [][]game = new int [SIZE][SIZE];
boolean getResult = false;
System.out.println("Please enter your X or Y");
for(int i=0;i<game.length;i++) {
for(int k=0;k<game[i].length;k++) {
game[i][k]=in.nextInt();
}
}
for(int i=0;i<game.length;i++) {//判断主对角线
if(game[i][i]==1) {
numOfX++;
}
else {
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
}
}
if(!getResult) {//判断副对角线
numOfX=0;
numOfY=0;
for(int i=0;i<game.length;i++) {
if(game[i][SIZE-1-i]==1) {
numOfX++;
}
else {
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
}
}
}
if(!getResult) {//判断行
for(int i=0;i<game.length;i++) {
numOfX=0;
numOfY=0;
for(int k=0;k<game[i].length;k++) {
if(game[i][k]==1)
numOfX++;
else
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
break;
}
}
}
if(!getResult) {//判断列
for(int i=0;i<SIZE;i++) {
numOfX=0;
numOfY=0;
for(int k=0;k<SIZE;k++) {
if(game[k][i]==1)
numOfX++;
else
numOfY++;
}
if(numOfX==SIZE || numOfY==SIZE) {
getResult=true;
break;
}
}
}
if(getResult) {
if(numOfX==SIZE) {
System.out.println("X win this game!");
}
else {
System.out.println("Y win this game!");
}
}
else {
System.out.println("draw!");
}
}
}
相关文章推荐
- 极大极小博弈树的简洁(附Tic-Tac-Toe源码)
- Java入门之tic-tac-toe
- [Codeforces]C. Tic-tac-toe
- Minimax-Tic Tac Toe
- 348. Design Tic-Tac-Toe
- python 井字棋(Tic Tac Toe)
- LeetCode "Design Tic-Tac-Toe"
- Codeforces 3 C. Tic-tac-toe
- POJ 2361 Tic Tac Toe 模拟
- Tic-Tac-Toe
- UVa 10363 - Tic Tac Toe
- POJ 3075 Tic-Tac-Toe 笔记
- Design Tic-Tac-Toe
- Java源码初学_HashSet&LinkedHashSet
- Common Lisp 语言编写的Tic-Tac-Toe
- 【Light OJ】No More Tic-tac-toe (博弈 SG函数)
- Problem A. Tic-Tac-Toe-Tomek【解题报告】
- (fzu) Problem L Tic-Tac-Toe(水)
- Tic-Tac-Toe(三子连)(总结规律)
- Analysis On Tic-Tac-Toe