Java實(shí)現(xiàn)三子棋小游戲簡易版
本文實(shí)例為大家分享了Java實(shí)現(xiàn)三子棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲操作如圖示:
原理:
主要借助3x3的二維數(shù)組
實(shí)現(xiàn):
1、Main–主程序
public class Main { ? ? public static void main(String[] args) { ? ? ? Player player=new Player();//玩家 ? ? ? Game game=new Game(player); ? ? ? //一次循環(huán)代表一次游戲 ? ? ? while (true){ ? ? ? ? ? game.Init();//初始化 ? ? ? ? ? game.play();//開始游戲 ? ? ? ? ? game.destory();//釋放 ? ? ? ? ? boolean q=player.queryContinue(); ? ? ? ? ? //一局游戲結(jié)束后,詢問用戶是否開始下一局游戲 ? ? ? ? ? if (!q){ ? ? ? ? ? ? ? System.out.println("歡迎下次繼續(xù)游戲!"); ? ? ? ? ? ? ? break; ? ? ? ? ? } ? ? ? } ? ? } }
2、AI–電腦
import java.util.Random; //返回AI對(duì)象的落子位置 //用數(shù)組表示 第幾行第幾列 從0開始 //所以有效范圍是[0,2] public class AI { ? ? private final Random random=new Random(); ? ? public int[] getPosition(){ ? ? ? ? int r=random.nextInt(3);//生成[0,2]的隨機(jī)整數(shù) 0 1 2 ? ? ? ? int c=random.nextInt(3); ? ? ? ? return new int[]{r,c}; ? ? } }
3、Player–玩家
import java.util.Scanner; //返回玩家落子位置 //用數(shù)組表示 第幾行第幾列 從0開始 //所以有效范圍是[0,2] public class Player { ? ? private final Scanner sc=new Scanner(System.in); ? ? public int[] getPosition(){ ? ? ? ? System.out.println("請(qǐng)輸入要落子的位置,行列有效取值范圍為[0,2]"); ? ? ? ? System.out.print(">>>"); ? ? ? ? int r,c; ? ? ? ?while (true){ ? ? ? ? ? ?System.out.print(">>>"); ? ? ? ? ? ?r=sc.nextInt(); ? ? ? ? ? ?c=sc.nextInt(); ? ? ? ? ? ?if (r>=0&&r<=2&&c>=0&&c<=2){ ? ? ? ? ? ? ? ?break; ? ? ? ? ? ?} ? ? ? ? ? ?System.out.println("行列有效范圍為[0,2],請(qǐng)重新輸入"); ? ? ? ?} ? ? ? ? return new int[]{r,c}; ? ? } ? ? //詢問用戶是否繼續(xù)下一局 ? ? public boolean queryContinue(){ ? ? ? ? System.out.println("本局游戲結(jié)束,開始新游戲請(qǐng)輸入true,否則輸入false"); ? ? ? ? System.out.print(">>>"); ? ? ? ? return sc.nextBoolean(); ? ? } }
4、ChessBoard–棋盤
import java.util.Arrays; //棋盤 用來實(shí)例化對(duì)象 //棋盤對(duì)象 //功能 1、落子 2、判斷棋盤狀態(tài) public class ChessBoard { ? ?private static final int empty=0;//空白位置用0表示 ? ?private static final int circle=1;//落子為o的位置 ? ?private static final int cross=2;//落子為x的位置 ? ?private final int[][] array={ ? ? ? ? ? ?{empty,empty,empty}, ? ? ? ? ? ?{empty,empty,empty}, ? ? ? ? ? ?{empty,empty,empty} ? ?}; ? ?public boolean moveCircleAt(int row,int column){//落一個(gè)o ? ? ? if (array[row][column]!=empty){ //落子前需要先判斷該位置是否為空白 ? ? ? ? ?return false; ? ? ? } ? ? ? array[row][column]=circle; ? ? ? return true; ? ?} ? ?public boolean moveCrossAT(int row,int column){//某個(gè)位置落個(gè)x ? ? ? if (array[row][column]!=empty){ ? ? ? ? ?return false; ? ? ? } ? ? ? array[row][column]=cross; ? ? ? return true; ? ?} ? ?//棋盤的四種狀態(tài) ? ?public static final int CIRCLE_WIN=0;//執(zhí)o者贏 ?//三橫三豎兩對(duì)角成直線 ? ?public static final int CROSS_WIN=1;//執(zhí)x者贏 ? ?public static final int DRAW=2;//平局 //沒有成直線 但無落子位置了 ? ? public static final int CONTINUE=3;//繼續(xù) ? ?public int getState(){//得到棋盤的狀態(tài) ? ? ? //判斷行 ? ? ? for (int i=0;i<3;i++){ ? ? ? ? ?if(array[i][0]==array[i][1]&&array[i][1]==array[i][2]){ ? ? ? ? ? ? if (array[i][0]==circle){ ? ? ? ? ? ? ? ?System.out.println("恭喜你贏了!"); ? ? ? ? ? ? ? ?return CIRCLE_WIN; ? ? ? ? ? ? } ? ? ? ? ? ? else if (array[i][0]==cross){ ? ? ? ? ? ? ? ?System.out.println("很遺憾你輸了!"); ? ? ? ? ? ? ? ?return CROSS_WIN; ? ? ? ? ? ? } ? ? ? ? ?} ? ? ? } ? ? ? for (int i=0;i<3;i++){ ? ? ? ? ?if (array[0][i]==array[1][i]&&array[1][i]==array[2][i]){ ? ? ? ? ? ? if (array[0][i]==circle){ ? ? ? ? ? ? ? ?System.out.println("恭喜你贏了!"); ? ? ? ? ? ? ? ?return CIRCLE_WIN; ? ? ? ? ? ? } ? ? ? ? ? ? else if(array[0][i]==cross){ ? ? ? ? ? ? ? ?System.out.println("很遺憾你輸了!"); ? ? ? ? ? ? ? ?return CROSS_WIN; ? ? ? ? ? ? } ? ? ? ? ?} ? ? ? } ? ? ? //正負(fù)對(duì)角線 ? ? ? //正負(fù)對(duì)角線 ? ? ? if ((array[0][0]==array[1][1]&&array[1][1]==array[2][2]) ? ? ? ? ? ? ? ||(array[0][2]==array[1][1]&&array[1][1]==array[2][0])){ ? ? ? ? ?if (array[1][1]==circle){ ? ? ? ? ? ? System.out.println("恭喜你贏了!"); ? ? ? ? ? ? return CIRCLE_WIN; ? ? ? ? ?} ? ? ? ? ?else if (array[1][1]==cross){ ? ? ? ? ? ? System.out.println("很遺憾你輸了!"); ? ? ? ? ? ? return CROSS_WIN; ? ? ? ? ?} ? ? ? } ? ? ? //無獲勝 ? ? ? for (int i=0;i<3;i++){ ? ? ? ? for (int j=0;j<3;j++){ ? ? ? ? ? ?if (array[i][j]==empty){ ? ? ? ? ? ? ? return CONTINUE; ? ? ? ? ? ?} ? ? ? ? } ? ? ? } ? ? ? return DRAW;//無獲勝也無空白 平局 ? ?} ? ?private static String show(int i){ //顯示 ? ? ? switch (i){ ? ? ? ? ?case empty: ? ? ? ? ? ? return " "; ? ? ? ? ?case circle: ? ? ? ? ? ? return "o"; ? ? ? ? ?case cross: ? ? ? ? ? ? return "x"; ? ? ? ? ?default: ? ? ? ? ? ? return "1"; ? ? ? } ? ?} ? ?@Override ? ?public String toString() { ? ? ? String s="---------\n"; ? ? ? for (int i=0;i<2;i++){ //前兩行 ? ? ? ? ?s+=String.format("|%s|%s|%s|\n",show(array[i][0]),show(array[i][1]),show(array[i][2])); ? ? ? ? ?s+="---------\n"; ? ? ? } ? ? ? //最后一行 ? ? ? s+=String.format("|%s|%s|%s|\n",show(array[2][0]),show(array[2][1]),show(array[2][2])); ? ? ? s+="---------"; ? ? ? return s; ? ?} ? ?public void reset() { ? ? ? for (int i=0;i<3;i++){ ? ? ? ? ?Arrays.fill(array[i],empty);//所有位置再次設(shè)置為空白 ? ? ? } ? ?} }
5、Game–游戲
import java.util.Arrays; public class Game { ? ? private final ChessBoard chessboard; ? ? private final Player player; ? ? private final AI ai; ? ? public Game(Player player){ ? ? ? ? this.chessboard=new ChessBoard(); ? ? ? ? this.player=player; ? ? ? ? this.ai=new AI(); ? ? } ? ? //初始化 ? ? public void Init(){ ? ? ? ? System.out.println("歡迎進(jìn)入三子棋游戲"); ? ? ? ? System.out.println(chessboard); ? ? } ? ? //回合制游戲,游戲主流程 ? ? public void play(){ ? ? ? ? while (true){ ?//一次循環(huán)=player回合+AI回合 ? ? ? ? ? ?if (playerTurn()){//玩家回合 ? ? ? ? ? ? ? ?break; ? ? ? ? ? ?} ? ? ? ? ? ?if (aiTurn()){//ai回合 ? ? ? ? ? ? ? ?break; ? ? ? ? ? ?} ? ? ? ? } ? ? } ? ? private boolean aiTurn() { ? ? ? ? System.out.println("AI回合:"); ? ? ? ? while (true) { ? ? ? ? ? ? int[] rc=ai.getPosition(); ? ? ? ? ? ? int row=rc[0]; ? ? ? ? ? ? int column=rc[1]; ? ? ? ? ? ? if(chessboard.moveCrossAT(row, column)){ ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? System.out.println(chessboard); ? ? ? ? return chessboard.getState()!=ChessBoard.CONTINUE; ? ? } ? ? private boolean playerTurn() { ? ? ? ? System.out.println("玩家回合:"); ? ? ? ? while (true) { ? ? ? ? ? ? int[] rc=player.getPosition(); ? ? ? ? ? ? int row=rc[0]; ? ? ? ? ? ? int column=rc[1]; ? ? ? ? ? ? if(chessboard.moveCircleAt(row, column)){ ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? System.out.println("該位置已經(jīng)有棋子,請(qǐng)重新選擇位置"); ? ? ? ? } ? ? ? ? System.out.println(chessboard); ? ? ? ? return chessboard.getState()!=ChessBoard.CONTINUE; ? ? } ? ? //新一局游戲開始時(shí) 游戲的界面需要重置 否則會(huì)是上一局游戲的結(jié)局界面 ? ? public void destory(){ ? ? ? ? chessboard.reset(); ? ? } }
運(yùn)行結(jié)果:
1、測試落子位置合法性
2、測試棋盤狀態(tài)/輸贏
3、測試平局
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SSH框架網(wǎng)上商城項(xiàng)目第14戰(zhàn)之商城首頁UI的設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第14戰(zhàn)之商城首頁UI的設(shè)計(jì),感興趣的小伙伴們可以參考一下2016-06-06SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Java利用反射動(dòng)態(tài)設(shè)置對(duì)象字段值的實(shí)現(xiàn)
橋梁信息維護(hù)需要做到字段級(jí)別的權(quán)限控制,本文主要介紹了Java利用反射動(dòng)態(tài)設(shè)置對(duì)象字段值的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01java多線程應(yīng)用實(shí)現(xiàn)方法
以前沒有寫筆記的習(xí)慣,現(xiàn)在慢慢的發(fā)現(xiàn)及時(shí)總結(jié)是多么的重要了,呵呵。雖然才大二,但是也快要畢業(yè)了,要加油2012-11-11IDEA配置tomcat的方法、IDEA配置tomcat運(yùn)行web項(xiàng)目詳解
這篇文章主要介紹了IDEA配置tomcat的方法、IDEA配置tomcat運(yùn)行web項(xiàng)目詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07