Java黑皮书课后题第8章:8.9(井字游戏)玩家使用各自标志标记3*3网格中的某个空格,当一个玩家在网格的水平、垂直或对角线方向标记了三个相同的标记时,游戏结束,该玩家获胜。创建一个玩井字游戏的程序
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第8章:8.9(井字游戏)玩家使用各自标志标记3*3网格中的某个空格,当一个玩家在网格的水平、垂直或对角线方向标记了三个相同的标记时,游戏结束,该玩家获胜。创建一个玩井字游戏的程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
***8.9(井字游戲)玩家使用各自標志標記3*3網格中的某個空格,當一個玩家在網格的水平、垂直或對角線方向標記了三個相同的標記時,游戲結束,該玩家獲勝。創建一個玩井字游戲的程序
- 題目
- 題目描述與運行示例
- 破題
- 代碼
題目
題目描述與運行示例
8.9(井字游戲)玩家使用各自標志標記3*3網格中的某個空格,當一個玩家在網格的水平、垂直或對角線方向標記了三個相同的標記時,游戲結束,該玩家獲勝。創建一個玩井字游戲的程序,提示用戶交替輸入X和O標記,當收入標記時,程序在控制臺上重新顯示棋盤,然后確定游戲的狀態(獲勝、平局或繼續)。下面是一個運行示例:
----------------- | | | | ----------------- | | | | ----------------- | | | | ----------------- Enter a row (0, 1, or 2) for player X: 1 Enter a column (0, 1, or 2) for player X: 1 ----------------- | | | | ----------------- | | X | | ----------------- | | | | ----------------- Enter a row (0, 1, or 2) for player O: 1 Enter a column (0, 1, or 2) for player O: 2 ----------------- | | | | ----------------- | | X | O | ----------------- | | | | ----------------- Enter a row (0, 1, or 2) for player X: 0 Enter a column (0, 1, or 2) for player X: 1 ----------------- | | X | | ----------------- | | X | O | ----------------- | | | | ----------------- Enter a row (0, 1, or 2) for player O: 2 Enter a column (0, 1, or 2) for player O: 1 ----------------- | | X | | ----------------- | | X | O | ----------------- | | O | | ----------------- Enter a row (0, 1, or 2) for player X: 2 Enter a column (0, 1, or 2) for player X: 2 ----------------- | | X | | ----------------- | | X | O | ----------------- | | O | X | ----------------- Enter a row (0, 1, or 2) for player O: 0 Enter a column (0, 1, or 2) for player O: 0 ----------------- | O | X | | ----------------- | | X | O | ----------------- | | O | X | ----------------- Enter a row (0, 1, or 2) for player X: 2 Enter a column (0, 1, or 2) for player X: 0 ----------------- | O | X | | ----------------- | | X | O | ----------------- | X | O | X | ----------------- Enter a row (0, 1, or 2) for player O: 0 Enter a column (0, 1, or 2) for player O: 2 ----------------- | O | X | O | ----------------- | | X | O | ----------------- | X | O | X | ----------------- Enter a row (0, 1, or 2) for player X: 1 Enter a column (0, 1, or 2) for player X: 0 ----------------- | O | X | O | ----------------- | X | X | O | ----------------- | X | O | X | -----------------破題
本題可以使用一個二維int型數組記錄用戶輸入標記
分為如下幾個部分:主方法(使用循環調用其他方法,如果有勝負時則停止運行),輸出棋盤方法(接收數組并將數組輸出到控制臺),接收用戶輸入(接收位置并根據奇偶次判斷需要給數組賦什么值),判斷誰勝誰負的方法(如果獲勝則要停止主方法程序運行)
代碼
import java.util.Scanner;public class Test8_9 {public static void main(String[] args) {// 用戶坐標Scanner input = new Scanner(System.in);int user_input1 = 0, user_input2 = 0;// 如果有獲勝方則改為falseboolean bool_have_result = true;// 棋盤int[][] chessboard = new int[3][3];// 計數變量int count = 0;//輸出棋盤output(chessboard);// 正文while (bool_have_result){//計數變量+1count++;//接收輸入if (count % 2 == 1){System.out.print("Enter a row (0, 1, or 2) for player X: ");user_input1 = input.nextInt();System.out.print("Enter a column (0, 1, or 2) for player X: ");user_input2 = input.nextInt();}else{System.out.print("Enter a row (0, 1, or 2) for player O: ");user_input1 = input.nextInt();System.out.print("Enter a column (0, 1, or 2) for player O: ");user_input2 = input.nextInt();}chessboard[user_input1][user_input2] = count % 2 + 1;//輸出棋盤output(chessboard);//判斷有沒有獲勝方int result = no_result(chessboard);if (result != 0){//有獲勝方bool_have_result = false;if (result == 1) System.out.print("x player won");else if (result == -1) System.out.print("o player won");}}}//輸出棋盤方法public static void output(int[][] cb){System.out.println("-----------------");for (int i = 0 ; i < cb.length ; i++){for (int j = 0 ; j < cb[i].length ; j++){if (cb[i][j] == 2) System.out.print("| X ");else if (cb[i][j] == 1) System.out.print("| O ");else System.out.print("| ");}System.out.println("|");System.out.println("-----------------");}}//判斷有沒有獲勝方(x勝返回1,o勝返回-1,全填滿2,還需要繼續0)public static int no_result(int[][] cb){//全填滿boolean full = true;for (int i = 0 ; i < cb.length ; i++){for (int j = 0 ; j < cb[i].length ;j++){if (cb[i][j] == 0){full = false;}}}if (full) return 2;// 有勝利方int[] diagonal1 = new int[cb.length];int[] diagonal2 = new int[cb.length];for (int i = 0 ; i < cb.length ; i++){diagonal1[i] = cb[i][i];diagonal2[i] = cb[i][cb.length - i - 1];}int temp = 0;boolean bool = true;for (int i = 0 ; i < cb.length ; i++){temp = cb[i][0];for (int j = 1 ; j < cb[i].length ;j++){if (temp != cb[i][j]) bool = false;}}// 判斷是x還是oif (bool && temp == 1)return -1;else if (bool && temp == 2)return 1;// 還需要繼續return 0;} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第8章:8.9(井字游戏)玩家使用各自标志标记3*3网格中的某个空格,当一个玩家在网格的水平、垂直或对角线方向标记了三个相同的标记时,游戏结束,该玩家获胜。创建一个玩井字游戏的程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第8章:*8.8(所
- 下一篇: Java黑皮书课后题第8章:*8.10(