Java實現(xiàn)三子棋小游戲
之前在學習C語言的時候,做過一個三子棋的小游戲,最近開始學習Java,就想著能不能用Java再把之前的練習重新實現(xiàn)一邊,既然有這個想法了,那就開始行動啦~。
再寫的過程中,能感覺到面向過程語言和面向對象語言的一些差異。最讓我頭疼的是類的設計,感覺不僅得考慮功能得實現(xiàn),還需要考慮類之間得邏輯關系,函數(shù)的功能是單一的,但函數(shù)與函數(shù)之間談不上什么關系,你要用的上我,你就用,不用就拉倒。類在設計的時候需要考慮將哪些方法封裝在哪個類當中,封裝位置不合適,盡管也可能實現(xiàn)功能,但總感覺怪怪的。自己最后的代碼其實還是按照之前C的設計思路寫的,只不過把其中一些內(nèi)容抽象成了類,然后在其中封裝了對應的方法。
三子棋介紹三子期是黑白棋的一種。三子棋是一種民間傳統(tǒng)游戲,又叫九宮棋、圈圈叉叉、一條龍、井字棋等。將正方形對角線連起來,相對兩邊依次擺上三個雙方棋子,只要將自己的三個棋子走成一條線,對方就算輸了。但是,有很多時候會出現(xiàn)和棋的情況。
三子棋規(guī)則玩家和電腦交替在棋盤中落下一個棋子,直到一方勝利或者平局,則結束游戲。
當棋盤上同一行或者同一列或者對角線的三個棋子都相同時,棋子所屬方勝利。
當棋盤已滿還未分出勝負,則平局。
具體實現(xiàn)測試類
用來測試程序的各項功能,其中也包括了程序的運行邏輯
public class BoardTest { public static void main(String[] args) {while(true){ showMenu(); // 顯示棋盤 boolean isPlay = Utility.readMenuSelection(); // 讀取玩家輸入 if(isPlay){ // isPlay == true 進入游戲System.out.println('歡迎來到三子棋小游戲!');Board board = new Board(); // 創(chuàng)建棋盤對象Player player = new Player(); // 創(chuàng)建玩家對象Computer computer = new Computer(); // 創(chuàng)建電腦對象board.initBoard(); // 初始化棋盤char flag = ’ ’; // 接收棋局判斷的返回值// 博弈過程while(true){ board.showBoard(); // 顯示棋盤 System.out.print('請輸入落棋點(x,y):>'); player.moveChess(board); // 玩家走 flag = board.isWin(); if(flag != ’c’){ // 將玩家和電腦的棋子傳入,判斷當前棋局的形式break; // != ’c’ 則跳出循環(huán) } computer.moveChess(board); // 電腦走 flag = board.isWin(); if(flag != ’c’){ // 將玩家和電腦的棋子傳入,判斷當前棋局的形式break; // != ’c’ 則跳出循環(huán) }}if(flag == player.getChess()){ System.out.println('玩家贏!');} else if (flag == computer.getChess()){ System.out.println('電腦贏!');} else { System.out.println('平局!');} } else { // 否則 再次確認玩家是否要離開System.out.println('真的要離開嘛?(Y/N):');boolean isExit = Utility.readConfirmSelection(); // 讀取玩家輸入if(isExit){ // isExit == true 退出游戲 System.out.println('退出成功!'); break;} }} } // 游戲菜單 public static void showMenu(){System.out.println('============================================');System.out.println('==========1. play===========');System.out.println('==========2. exit===========');System.out.println('============================================');System.out.print('請輸入選擇:>'); }}
棋盤類
屬性方法概述
// 屬性:private int row; // 行private int col; // 列private char[][] board; // 棋盤數(shù)組// 方法:public void initBoard() {...} // 用來初始化棋盤,講棋盤二維數(shù)組中的每個元素賦值為’ ’public void showBoard() {...} // 用來顯示棋盤public char isWin() {...} // 判斷當前棋局的形勢private boolean isFull(){...} // 判斷棋盤是否已滿// getter方法,因為不允許設置棋盤屬性,沒有setter方法public int getRow() {...} // 返回行public int getCol() {...} // 返回列public char[][] getBoard() {...} // 返回棋盤數(shù)組// 構造器public Board() {...} // 空參構造器,在其中默認設置了行列,并new了棋盤數(shù)組對象完整代碼
public class Board { private int row; // 行 private int col; // 列 private char[][] board; // 棋盤數(shù)組 /** * @description: 構造器,用來設置棋盤大小 * @parameter row, 表示行數(shù); col, 表示列數(shù) */ public Board() {row = 3;col = 3;board = new char[row][col]; } /** * @description: 用來初始化棋盤,講棋盤二維數(shù)組中的每個元素賦值為’ ’ */ public void initBoard() {for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) {board[i][j] = ’ ’; }} } /** * @description: 用來顯示棋盤 */ public void showBoard() {for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) {if (j < col - 1) { System.out.print(' ' + board[i][j] + ' |');} else { System.out.print(' ' + board[i][j] + ' ');} } System.out.println(); if (i < row - 1) {for (int k = 0; k < col; k++) { if (k < col - 1) {System.out.print('---|'); } else {System.out.print('---'); }}System.out.println(); }} } /** * @description 判斷當前棋局的形勢 * @return 返回 玩家的棋子 玩家贏, 返回 電腦的棋子 電腦贏 * 返回 ’q’ 平局 , 返回 ’c’ 繼續(xù) */ public char isWin() {// 判斷同一行是否相同for (int i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ’ ’) {return board[i][0]; // 返回該行的一個棋子 }}// 判斷同一列是否相同for(int i = 0; i < col; i++){ if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ’ ’) {return board[i][0]; // 返回該列的一個棋子 }}// 判斷對角線是否相同if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ’ ’ || board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ’ ’){ return board[1][1];// 返回對角線中間的棋子}// 判斷棋盤是否已滿if(isFull()){ return ’q’; // 已滿返回’q’} else { return ’c’; // 未滿返回’c’} } /** * @description 判斷棋盤是否已滿 * @return true 表示已滿 false 表示未滿 */ private boolean isFull(){for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) {if(board[i][j] == ’ ’){ return false; // 未滿返回false} }}return true; // 已滿返回true } public int getRow() {return row; } public int getCol() {return col; } public char[][] getBoard() {return board; }}
用戶類
屬性方法概述
// 屬性:private char chess; // 玩家的棋子// 方法:public void moveChess(Board board){...} // 玩家下棋,講棋子放到指定位置// getter方法public char getChess() {...} // 用來返回玩家的棋子// 構造器:public Player() {...} // 空參構造器,默認設置了代表玩家棋子的字符完整代碼
public class Player { private char chess; // 棋子 /** * @description 空參構造器,用來設置用戶的棋子 */ public Player() {chess = ’*’; } /** * @description 玩家下棋,講棋子放到指定位置 * @param board 接收使用的棋盤數(shù)組 */ public void moveChess(Board board){Scanner sc = new Scanner(System.in);while(true){ int x = sc.nextInt(); // 接收x坐標 int y = sc.nextInt(); // 接收y坐標 boolean isFlag = Utility.readPosition(x,y,board); // 判斷位置是否合適 if(isFlag){ // 合適則將玩家棋子存入棋盤的對應位置中board.getBoard()[x-1][y-1] = chess; return; } System.out.print('輸入有誤,請重新輸入:>');} } /** * @return 返回玩家的棋子 */ public char getChess() {return chess; }}
電腦類
屬性方法概述
// 屬性:private char chess; // 電腦的棋子// 方法:public void moveChess(Board board){...} // 電腦下棋,在棋盤中隨機位置放入一個棋子// getter方法:public char getChess() {...} // 返回電腦的棋子// 構造器:public Computer() {...} // 空參構造,默認設置了代表電腦棋子的字符完整代碼
import java.util.Scanner;public class Computer { private char chess; // 電腦的棋子 /** * @description 用來設置電腦的棋子 */ public Computer() {chess = ’#’; } /** * @description 在棋盤中隨機位置放入一個棋子 * @param board 接收使用的棋盤數(shù)組 */ public void moveChess(Board board){Scanner sc = new Scanner(System.in);while(true){ int x = (int)(Math.random() * 3); int y = (int)(Math.random() * 3); boolean isFlag = Utility.readPosition(x+1,y+1,board); if(isFlag){board.getBoard()[x][y] = chess;return; }} } /** * * @return 返回電腦的棋子 */ public char getChess() {return chess; }}
工具類
提供了static方法,主要用來接收玩家的輸入,判斷輸入是否合適。
屬性方法概述
// 方法 public static boolean readMenuSelection() {...} // 讀取玩家進入菜單界面的輸入選項,并進行判斷。public static boolean readConfirmSelection() {...} // 讀取玩家確認選項的輸入,’Y’表示確認 ’N’表示否認public static boolean readPosition(int x, int y, Board board){...} // 判斷玩家輸入的x y 坐標是否合理,合理位置返回true, 不合理位置返回false完整代碼
import java.util.Scanner;public class Utility { /** * @description: 讀取用戶輸入的數(shù)字,并進行判斷 * @return 輸入1則返回true, 輸入2 則返回false */ public static boolean readMenuSelection() {int num = new Scanner(System.in).nextInt();while (true) { if (num == 1) {return true; } else if (num == 2) {return false; } else {System.out.print('輸入有誤,請重新輸入:>'); }} } /** * @description: 讀取用戶輸入的字符,并做出判斷 * @return 輸入’Y’ 返回true, 輸入’N’ 返回false */ public static boolean readConfirmSelection() {String str = new Scanner(System.in).next();while(true){ if(str.length() == 1){char ch = str.toUpperCase().charAt(0); // 獲取該字符System.out.println(ch);if(ch == ’Y’){ return true;} else if( ch == ’N’){ return false;} } System.out.print('輸入有誤,請重新輸入:>');} } /** * @description 判斷用戶輸入的落棋點是否在合理位置 * @param x x坐標 * @param y y坐標 * @param board 使用的棋盤對象 * @return 合理位置返回true, 不合理位置返回false */ public static boolean readPosition(int x, int y, Board board){if(1 <= x && x <= board.getRow() && 1 <= y && y <= board.getCol() && board.getBoard()[x - 1][y - 1] == ’ ’){ return true;}return false; }}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
