JAVA三星题之playing a TicTacToe game
来源:互联网 发布:南京市软件行业协会 编辑:程序博客网 时间:2024/04/30 16:12
原题:In a game if TicTacToe,two players take turns marking an available cell in a 3x3 grid with their respective tokens(either X or O).When one player has placed three tokens in a horizontal,vertical or diagonal row on the gird,the game is over and that player has won,A draw(no winner) occurs when all the cells on the gird have been filled with tokens and neither player has achiceved a win,Create a program for playing TicTacToe.The program prompts two players to enter X token is enterd,the program redisplays the board on the console and deter=mines the of the game (win,draw,or continue)。
题目来源:
题目选自《JAVA程序语言设计》 P254-7.9***
代码如下:
import java.util.Scanner;public class Main {public static int[][] Chess=new int[3][3]; //建立九宫格public static int player=1;//1 为X,-1为Opublic static int count=0;//计数器,记录九宫格上的棋子数public static void main(String[] args) {Scanner input=new Scanner(System.in); while(true) { //建立游戏循环 while(true) { //建立输入循环 System.out.print("Enter a row(0, 1 or 2) for player"); if(player == 1) System.out.print(" X: "); else System.out.print(" O: "); int row = input.nextInt(); //输入行row上的位置 System.out.print("Enter a column(0, 1 or 2) for player"); if(player == 1) System.out.print(" X: "); else System.out.print(" O: "); int column = input.nextInt(); //输入列column上的位置 if (Judgingrange(row, column, player) == true) { //判断输入是否在九宫格内且输入位置没有棋子, break; //如果符合则跳出输入循环 } } ShowCheckerboard(); //显示当前九宫格局势 if (JudgingWin(player) == true) { //判断是否有人赢 if (player==1) System.out.println("X player win"); else System.out.println("O player win"); System.out.print("Would you like to continue? (Y or N):"); String choose = input.next(); if (choose.equals("Y")) { for (int i=0; i<3 ; i++) for (int j=0; j<3 ; j++) Chess[i][j]=0; count=0; player=1; continue; }//重新开始游戏 else if (choose.equals("N")) { break; }//跳出游戏循环 } if (count == 9) { //如果九宫格每个格上都有棋子 System.out.println("X and O draw"); //则是平局 System.out.print("Would you like to continue? (Y or N):"); String choose = input.next(); if (choose.equals("Y")) { for (int i=0; i<3 ; i++) for (int j=0; j<3 ; j++) Chess[i][j]=0; count=0; player=1; continue; }//重新开始游戏 else if (choose.equals("N")) { break; }//跳出游戏循环 } player=-player; } System.out.println("Game Over!");}public static boolean Judgingrange(int x,int y, int num){if (x < 0 || x > 2 || y < 0 || y > 2 ) { //检查是否越界 System.out.println("Input out, please input again."); return false; } if (Chess[x][y] == 0) { //如果当前位置没有棋子 Chess[x][y] = num; count++; return true; } else { //如果当前位置有棋子 System.out.println("There's already a piece, Input out, please input again."); return false; } }public static boolean JudgingWin(int num) //判断是否X和O是否有赢{for (int i = 0; i < 3; i++) { //检查行和列上是否有三个一样的棋子if (Chess[0][i] == Chess[1][i] && Chess[1][i] == Chess[2][i] &&Chess[2][i] == num) //行{ return true; } else if (Chess[i][0] == Chess[i][1] && Chess[i][1] == Chess[i][2] && Chess[i][2] == num) //列 { return true; }}//检查主对角线和副对角线上是否有三个一样的棋子if (Chess[0][0] == Chess[1][1] && Chess[1][1] == Chess[2][2] && Chess[2][2] == num)//主对角线return true;else if (Chess[0][2] == Chess[1][1] && Chess[1][1] == Chess[2][0] && Chess[2][0] == num)//副对角线return true;return false;}public static void ShowCheckerboard()//输出九宫格局势{System.out.println("—————————————"); for (int i = 0; i < 3; ++i) { System.out.print("|"); for (int j = 0; j < 3; ++j) { if (Chess[i][j] == 1 ) System.out.print(" X |"); else if(Chess[i][j] == -1) System.out.print(" O |"); else System.out.print(" |"); } System.out.println(); System.out.println("—————————————"); } } }
运行结果:
/*output:case 1:Enter a row(0, 1 or 2) for player X: 1Enter a column(0, 1 or 2) for player X: 1—————————————| | | |—————————————| | X | |—————————————| | | |—————————————Enter a row(0, 1 or 2) for player O: 2Enter a column(0, 1 or 2) for player O: 1—————————————| | | |—————————————| | X | |—————————————| | O | |—————————————Enter a row(0, 1 or 2) for player X: 2Enter a column(0, 1 or 2) for player X: 2—————————————| | | |—————————————| | X | |—————————————| | O | X |—————————————Enter a row(0, 1 or 2) for player O: 0Enter a column(0, 1 or 2) for player O: 0—————————————| O | | |—————————————| | X | |—————————————| | O | X |—————————————Enter a row(0, 1 or 2) for player X: 1Enter a column(0, 1 or 2) for player X: 2—————————————| O | | |—————————————| | X | X |—————————————| | O | X |—————————————Enter a row(0, 1 or 2) for player O: 0Enter a column(0, 1 or 2) for player O: 2—————————————| O | | O |—————————————| | X | X |—————————————| | O | X |—————————————Enter a row(0, 1 or 2) for player X: 1Enter a column(0, 1 or 2) for player X: 0—————————————| O | | O |—————————————| X | X | X |—————————————| | O | X |—————————————X player winWould you like to continue? (Y or N):NGame Over!case 2 :Enter a row(0, 1 or 2) for player X: 2Enter a column(0, 1 or 2) for player X: 3Input out, please input again.Enter a row(0, 1 or 2) for player X: 1Enter a column(0, 1 or 2) for player X: 1—————————————| | | |—————————————| | X | |—————————————| | | |—————————————Enter a row(0, 1 or 2) for player O: 0Enter a column(0, 1 or 2) for player O: 0—————————————| O | | |—————————————| | X | |—————————————| | | |—————————————Enter a row(0, 1 or 2) for player X: 2Enter a column(0, 1 or 2) for player X: 1—————————————| O | | |—————————————| | X | |—————————————| | X | |—————————————Enter a row(0, 1 or 2) for player O: 0Enter a column(0, 1 or 2) for player O: 1—————————————| O | O | |—————————————| | X | |—————————————| | X | |—————————————Enter a row(0, 1 or 2) for player X: 2Enter a column(0, 1 or 2) for player X: 1There's already a piece, Input out, please input again.Enter a row(0, 1 or 2) for player X: 2Enter a column(0, 1 or 2) for player X: 0—————————————| O | O | |—————————————| | X | |—————————————| X | X | |—————————————Enter a row(0, 1 or 2) for player O: 0Enter a column(0, 1 or 2) for player O: 2—————————————| O | O | O |—————————————| | X | |—————————————| X | X | |—————————————O player winWould you like to continue? (Y or N):NGame Over!case 3:Enter a row(0, 1 or 2) for player X: 1Enter a column(0, 1 or 2) for player X: 1—————————————| | | |—————————————| | X | |—————————————| | | |—————————————Enter a row(0, 1 or 2) for player O: 2Enter a column(0, 1 or 2) for player O: 2—————————————| | | |—————————————| | X | |—————————————| | | O |—————————————Enter a row(0, 1 or 2) for player X: 1Enter a column(0, 1 or 2) for player X: 0—————————————| | | |—————————————| X | X | |—————————————| | | O |—————————————Enter a row(0, 1 or 2) for player O: 1Enter a column(0, 1 or 2) for player O: 2—————————————| | | |—————————————| X | X | O |—————————————| | | O |—————————————Enter a row(0, 1 or 2) for player X: 0Enter a column(0, 1 or 2) for player X: 2—————————————| | | X |—————————————| X | X | O |—————————————| | | O |—————————————Enter a row(0, 1 or 2) for player O: 2Enter a column(0, 1 or 2) for player O: 0—————————————| | | X |—————————————| X | X | O |—————————————| O | | O |—————————————Enter a row(0, 1 or 2) for player X: 2Enter a column(0, 1 or 2) for player X: 1—————————————| | | X |—————————————| X | X | O |—————————————| O | X | O |—————————————Enter a row(0, 1 or 2) for player O: 0Enter a column(0, 1 or 2) for player O: 1—————————————| | O | X |—————————————| X | X | O |—————————————| O | X | O |—————————————Enter a row(0, 1 or 2) for player X: 0Enter a column(0, 1 or 2) for player X: 0—————————————| X | O | X |—————————————| X | X | O |—————————————| O | X | O |—————————————X and O drawWould you like to continue? (Y or N):NGame Over!*///~
1 0
- JAVA三星题之playing a TicTacToe game
- JAVA Playing a TicTacToe game
- JAVA编程训练——Game: playing a TicTacToe game
- Project: TicTacToe Game
- JAVA三星题之hangman
- Java ME, take a snapshot from a playing video
- Java三星题之显示日期
- JAVA三星题之Eight Queens
- JAVA三星题之Galton Box
- JAVA三星题之Financial tsunami
- JAVA三星题之connect four
- JAVA图形界面三星题之Hangman
- [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 1
- [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 2
- [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 3
- [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 4
- Develop one game for playing
- A. Playing with Dice
- Python 中文编码
- 嵌入式学习之,uboot的裁剪移植
- LightOJ 1025 - The Specials Menu (区间dp)
- 爬爬爬之路:OC语言(六) Block语法简介
- include指令和动作元素
- JAVA三星题之playing a TicTacToe game
- 第三部分 ARM裸机系列之教你学会刷操作系统
- Java 中泛型的全面解析
- hdu 3746 Cyclic Nacklace KMP中的nex[]应用
- poj 1602 昂贵的聘礼
- LeetCode——Peeking Iterator
- Help is needed for Dexter
- JQuery-Jquery的TreeGrid插件
- Nginx+Tomcat 做请求分发以及负载均衡