Java實(shí)現(xiàn)三子棋小游戲
# 前言
之前在學(xué)習(xí)C語(yǔ)言的時(shí)候,做過(guò)一個(gè)三子棋的小游戲,最近開始學(xué)習(xí)Java,就想著能不能用Java再把之前的練習(xí)重新實(shí)現(xiàn)一邊,既然有這個(gè)想法了,那就開始行動(dòng)啦~。
再寫的過(guò)程中,能感覺(jué)到面向過(guò)程語(yǔ)言和面向?qū)ο笳Z(yǔ)言的一些差異。最讓我頭疼的是類的設(shè)計(jì),感覺(jué)不僅得考慮功能得實(shí)現(xiàn),還需要考慮類之間得邏輯關(guān)系,函數(shù)的功能是單一的,但函數(shù)與函數(shù)之間談不上什么關(guān)系,你要用的上我,你就用,不用就拉倒。類在設(shè)計(jì)的時(shí)候需要考慮將哪些方法封裝在哪個(gè)類當(dāng)中,封裝位置不合適,盡管也可能實(shí)現(xiàn)功能,但總感覺(jué)怪怪的。自己最后的代碼其實(shí)還是按照之前C的設(shè)計(jì)思路寫的,只不過(guò)把其中一些內(nèi)容抽象成了類,然后在其中封裝了對(duì)應(yīng)的方法。
三子棋介紹
三子期是黑白棋的一種。三子棋是一種民間傳統(tǒng)游戲,又叫九宮棋、圈圈叉叉、一條龍、井字棋等。將正方形對(duì)角線連起來(lái),相對(duì)兩邊依次擺上三個(gè)雙方棋子,只要將自己的三個(gè)棋子走成一條線,對(duì)方就算輸了。但是,有很多時(shí)候會(huì)出現(xiàn)和棋的情況。
三子棋規(guī)則
玩家和電腦交替在棋盤中落下一個(gè)棋子,直到一方勝利或者平局,則結(jié)束游戲。
當(dāng)棋盤上同一行或者同一列或者對(duì)角線的三個(gè)棋子都相同時(shí),棋子所屬方勝利。
當(dāng)棋盤已滿還未分出勝負(fù),則平局。
具體實(shí)現(xiàn)
測(cè)試類
用來(lái)測(cè)試程序的各項(xiàng)功能,其中也包括了程序的運(yùn)行邏輯
public class BoardTest {
public static void main(String[] args) {
while(true){
showMenu(); // 顯示棋盤
boolean isPlay = Utility.readMenuSelection(); // 讀取玩家輸入
if(isPlay){ // isPlay == true 進(jìn)入游戲
System.out.println("歡迎來(lái)到三子棋小游戲!");
Board board = new Board(); // 創(chuàng)建棋盤對(duì)象
Player player = new Player(); // 創(chuàng)建玩家對(duì)象
Computer computer = new Computer(); // 創(chuàng)建電腦對(duì)象
board.initBoard(); // 初始化棋盤
char flag = ' '; // 接收棋局判斷的返回值
// 博弈過(guò)程
while(true){
board.showBoard(); // 顯示棋盤
System.out.print("請(qǐng)輸入落棋點(diǎn)(x,y):>");
player.moveChess(board); // 玩家走
flag = board.isWin();
if(flag != 'c'){ // 將玩家和電腦的棋子傳入,判斷當(dāng)前棋局的形式
break; // != 'c' 則跳出循環(huán)
}
computer.moveChess(board); // 電腦走
flag = board.isWin();
if(flag != 'c'){ // 將玩家和電腦的棋子傳入,判斷當(dāng)前棋局的形式
break; // != 'c' 則跳出循環(huán)
}
}
if(flag == player.getChess()){
System.out.println("玩家贏!");
} else if (flag == computer.getChess()){
System.out.println("電腦贏!");
} else {
System.out.println("平局!");
}
} else { // 否則 再次確認(rèn)玩家是否要離開
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("請(qǐng)輸入選擇:>");
}
}
棋盤類
屬性方法概述
// 屬性:
private int row; // 行
private int col; // 列
private char[][] board; // 棋盤數(shù)組
// 方法:
public void initBoard() {...} // 用來(lái)初始化棋盤,講棋盤二維數(shù)組中的每個(gè)元素賦值為' '
public void showBoard() {...} // 用來(lái)顯示棋盤
public char isWin() {...} // 判斷當(dāng)前棋局的形勢(shì)
private boolean isFull(){...} // 判斷棋盤是否已滿
// getter方法,因?yàn)椴辉试S設(shè)置棋盤屬性,沒(méi)有setter方法
public int getRow() {...} // 返回行
public int getCol() {...} // 返回列
public char[][] getBoard() {...} // 返回棋盤數(shù)組
// 構(gòu)造器
public Board() {...} // 空參構(gòu)造器,在其中默認(rèn)設(shè)置了行列,并new了棋盤數(shù)組對(duì)象
完整代碼
public class Board {
private int row; // 行
private int col; // 列
private char[][] board; // 棋盤數(shù)組
/**
* @description: 構(gòu)造器,用來(lái)設(shè)置棋盤大小
* @parameter row, 表示行數(shù); col, 表示列數(shù)
*/
public Board() {
row = 3;
col = 3;
board = new char[row][col];
}
/**
* @description: 用來(lái)初始化棋盤,講棋盤二維數(shù)組中的每個(gè)元素賦值為' '
*/
public void initBoard() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
board[i][j] = ' ';
}
}
}
/**
* @description: 用來(lái)顯示棋盤
*/
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 判斷當(dāng)前棋局的形勢(shì)
* @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]; // 返回該行的一個(gè)棋子
}
}
// 判斷同一列是否相同
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]; // 返回該列的一個(gè)棋子
}
}
// 判斷對(duì)角線是否相同
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]; // 返回對(duì)角線中間的棋子
}
// 判斷棋盤是否已滿
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() {...} // 用來(lái)返回玩家的棋子
// 構(gòu)造器:
public Player() {...} // 空參構(gòu)造器,默認(rèn)設(shè)置了代表玩家棋子的字符
完整代碼
public class Player {
private char chess; // 棋子
/**
* @description 空參構(gòu)造器,用來(lái)設(shè)置用戶的棋子
*/
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坐標(biāo)
int y = sc.nextInt(); // 接收y坐標(biāo)
boolean isFlag = Utility.readPosition(x,y,board); // 判斷位置是否合適
if(isFlag){ // 合適則將玩家棋子存入棋盤的對(duì)應(yīng)位置中
board.getBoard()[x-1][y-1] = chess;
return;
}
System.out.print("輸入有誤,請(qǐng)重新輸入:>");
}
}
/**
* @return 返回玩家的棋子
*/
public char getChess() {
return chess;
}
}
電腦類
屬性方法概述
// 屬性:
private char chess; // 電腦的棋子
// 方法:
public void moveChess(Board board){...} // 電腦下棋,在棋盤中隨機(jī)位置放入一個(gè)棋子
// getter方法:
public char getChess() {...} // 返回電腦的棋子
// 構(gòu)造器:
public Computer() {...} // 空參構(gòu)造,默認(rèn)設(shè)置了代表電腦棋子的字符
完整代碼
import java.util.Scanner;
public class Computer {
private char chess; // 電腦的棋子
/**
* @description 用來(lái)設(shè)置電腦的棋子
*/
public Computer() {
chess = '#';
}
/**
* @description 在棋盤中隨機(jī)位置放入一個(gè)棋子
* @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方法,主要用來(lái)接收玩家的輸入,判斷輸入是否合適。
屬性方法概述
// 方法
public static boolean readMenuSelection() {...} // 讀取玩家進(jìn)入菜單界面的輸入選項(xiàng),并進(jìn)行判斷。
public static boolean readConfirmSelection() {...} // 讀取玩家確認(rèn)選項(xiàng)的輸入,'Y'表示確認(rèn) 'N'表示否認(rèn)
public static boolean readPosition(int x, int y, Board board){...} // 判斷玩家輸入的x y 坐標(biāo)是否合理,合理位置返回true, 不合理位置返回false
完整代碼
import java.util.Scanner;
public class Utility {
/**
* @description: 讀取用戶輸入的數(shù)字,并進(jìn)行判斷
* @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("輸入有誤,請(qǐng)重新輸入:>");
}
}
}
/**
* @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("輸入有誤,請(qǐng)重新輸入:>");
}
}
/**
* @description 判斷用戶輸入的落棋點(diǎn)是否在合理位置
* @param x x坐標(biāo)
* @param y y坐標(biāo)
* @param board 使用的棋盤對(duì)象
* @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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)2048小游戲(可直接運(yùn)行)
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)2048小游戲的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
SpringMVC @RequestBody 為null問(wèn)題的排查及解決
這篇文章主要介紹了SpringMVC @RequestBody 為null問(wèn)題的排查及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Character.UnicodeBlock中cjk的說(shuō)明詳解
這篇文章主要為大家詳細(xì)介紹了Character.UnicodeBlock中cjk的說(shuō)明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
springmvc+shiro+maven 實(shí)現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理
Shiro 是一個(gè) Apache 下的一開源項(xiàng)目項(xiàng)目,旨在簡(jiǎn)化身份驗(yàn)證和授權(quán),下面通過(guò)實(shí)例代碼給大家分享springmvc+shiro+maven 實(shí)現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理,感興趣的朋友一起看看吧2017-09-09
使用Spring?Cloud?Stream處理Java消息流的操作流程
Spring?Cloud?Stream是一個(gè)用于構(gòu)建消息驅(qū)動(dòng)微服務(wù)的框架,能夠與各種消息中間件集成,如RabbitMQ、Kafka等,今天我們來(lái)探討如何使用Spring?Cloud?Stream來(lái)處理Java消息流,需要的朋友可以參考下2024-08-08
如何使用IDEA從SVN服務(wù)端檢出項(xiàng)目
這篇文章主要介紹了如何使用IDEA從SVN服務(wù)端檢出項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java中流的有關(guān)知識(shí)點(diǎn)詳解
今天小編就為大家分享一篇關(guān)于Java中流的有關(guān)知識(shí)點(diǎn)詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
MyBatis中SqlSession實(shí)現(xiàn)增刪改查案例
這篇文章主要介紹了MyBatis中SqlSession實(shí)現(xiàn)增刪改查案例,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Spring MVC訪問(wèn)靜態(tài)文件_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Spring MVC訪問(wèn)靜態(tài)文件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

