tic-tac-toe游戏
2017-10-22 13:59
756 查看
tic-tac-toe游戏
游戏描述:读入一个3X3的矩阵,矩阵中的数字为1 表示该位置上有一个X,为0表示为O。程序判断这个矩阵中是否有获胜的一方,输出表示获胜一方的字符X或O,或输出无人 获胜。import java.util.Scanner; public class Game { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); final int SIZE = 3; int[][] board = new int [SIZE][SIZE]; int num0fx =0; int num0fo =0; boolean gotResult = false; // 读入矩阵 for(int i=0;i<board.length;i++) { for(int j=0;j<board[i].length;j++) { board[i][j]=in.nextInt(); } } // 检查行 for(int i=0;i<SIZE;i++) { num0fx=0; num0fo=0; for(int j=0;j<SIZE;j++) { if(board[i][j]==1) { num0fx++; } else { num0fo++; } } if(num0fx==3||num0fo==3) { gotResult=true; } } // 检查列 if(!gotResult) { for(int i=0;i<SIZE;i++) { num0fx=0; num0fo=0; for(int j=0;j<SIZE;j++) { if(board[j][i]==1) { num0fx++; } else { num0fo++; } } if(num0fx==3||num0fo==3) { gotResult=true; } } } // 检查对角线 if(!gotResult) { num0fx=0; num0fo=0; for(int i=0;i<SIZE;i++) { if(board[i][i]==1) { num0fx++; } else { num0fo++; } } if(num0fx==3||num0fo==3) { gotResult=true; } } // 检查反对角线 if(!gotResult) { num0fx=0; num0fo=0; for(int i=0;i<SIZE;i++) { if(board[i][SIZE-i-1]==1) { num0fx++; } else { num0fo++; } } if(num0fx==3||num0fo==3) { gotResult=true; } } if(gotResult) { if(num0fx==SIZE) { System.out.println("X WIN"); } else { System.out.println("O WIN"); } } else { System.out.println("无人获胜"); } } }
相关文章推荐
- Python实现的井字棋(Tic Tac Toe)游戏示例
- 使用Python编写一个简单的tic-tac-toe游戏的教程
- tic-tac-toe游戏代码
- 二维数组应用举例:游戏“一担挑”(tic-tac-toe)
- Tic-Tac-Toe游戏
- Android 联网 tic-tac-toe 游戏
- Codeforces 3C. Tic-tac-toe
- Codeforces Round #454 (Div. 2, based on Technocup 2018 Elimination Round 4) B - Tic-Tac-Toe
- 井字棋(Tic-Tac-Toe)
- tic-tac-toe_game 井字棋游戏java源码(大一初学)
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
- codeforces 3C. Tic-tac-toe
- 【Light OJ】No More Tic-tac-toe (博弈 SG函数)
- Tic-Tac-Toe(三子连)(总结规律)
- 第八届福建省大学生程序设计竞赛-重现赛 L Tic-Tac-Toe
- FOJ Problem 2283 Tic-Tac-Toe(暴力枚举)——第八届福建省大学生程序设计竞赛-重现赛
- B. Tic-Tac-Toe
- Tic-Tac-Toe(三子连)(总结规律)
- 907B - Tic-Tac-Toe
- [Codeforces]C. Tic-tac-toe