Java黑皮书课后题第8章:8.9(井字游戏)玩家使用各自标志标记3*3网格中的某个空格,当一个玩家在网格的水平、垂直或对角线方向标记了三个相同的标记时,游戏结束,该玩家获胜。创建一个玩井字游戏的程序
***8.9(井字游戲)玩家使用各自標(biāo)志標(biāo)記3*3網(wǎng)格中的某個(gè)空格,當(dāng)一個(gè)玩家在網(wǎng)格的水平、垂直或?qū)蔷€方向標(biāo)記了三個(gè)相同的標(biāo)記時(shí),游戲結(jié)束,該玩家獲勝。創(chuàng)建一個(gè)玩井字游戲的程序
- 題目
- 題目描述與運(yùn)行示例
- 破題
- 代碼
題目
題目描述與運(yùn)行示例
8.9(井字游戲)玩家使用各自標(biāo)志標(biāo)記3*3網(wǎng)格中的某個(gè)空格,當(dāng)一個(gè)玩家在網(wǎng)格的水平、垂直或?qū)蔷€方向標(biāo)記了三個(gè)相同的標(biāo)記時(shí),游戲結(jié)束,該玩家獲勝。創(chuàng)建一個(gè)玩井字游戲的程序,提示用戶(hù)交替輸入X和O標(biāo)記,當(dāng)收入標(biāo)記時(shí),程序在控制臺(tái)上重新顯示棋盤(pán),然后確定游戲的狀態(tài)(獲勝、平局或繼續(xù))。下面是一個(gè)運(yùn)行示例:
----------------- | | | | ----------------- | | | | ----------------- | | | | ----------------- 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 | -----------------破題
本題可以使用一個(gè)二維int型數(shù)組記錄用戶(hù)輸入標(biāo)記
分為如下幾個(gè)部分:主方法(使用循環(huán)調(diào)用其他方法,如果有勝負(fù)時(shí)則停止運(yùn)行),輸出棋盤(pán)方法(接收數(shù)組并將數(shù)組輸出到控制臺(tái)),接收用戶(hù)輸入(接收位置并根據(jù)奇偶次判斷需要給數(shù)組賦什么值),判斷誰(shuí)勝誰(shuí)負(fù)的方法(如果獲勝則要停止主方法程序運(yùn)行)
代碼
import java.util.Scanner;public class Test8_9 {public static void main(String[] args) {// 用戶(hù)坐標(biāo)Scanner input = new Scanner(System.in);int user_input1 = 0, user_input2 = 0;// 如果有獲勝方則改為falseboolean bool_have_result = true;// 棋盤(pán)int[][] chessboard = new int[3][3];// 計(jì)數(shù)變量int count = 0;//輸出棋盤(pán)output(chessboard);// 正文while (bool_have_result){//計(jì)數(shù)變量+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;//輸出棋盤(pán)output(chessboard);//判斷有沒(méi)有獲勝方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");}}}//輸出棋盤(pán)方法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("-----------------");}}//判斷有沒(méi)有獲勝方(x勝返回1,o勝返回-1,全填滿2,還需要繼續(xù)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;// 還需要繼續(xù)return 0;} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第8章:8.9(井字游戏)玩家使用各自标志标记3*3网格中的某个空格,当一个玩家在网格的水平、垂直或对角线方向标记了三个相同的标记时,游戏结束,该玩家获胜。创建一个玩井字游戏的程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java黑皮书课后题第8章:*8.8(所
- 下一篇: Java黑皮书课后题第8章:*8.10(