本文主要是介绍三子棋简单小游戏的详细的实现(C语言和java两种),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
三子棋的实现
- 三子棋简介
- 具体实现
- C语言
- test.c
- game.h
- game.c
- 演示
- Java版本:
- 棋盘类:
- 玩家类:
- Ai类
- 游戏类
- 测试游戏:
三子棋简介
三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。
具体实现
C语言
需要测试test.c实现游戏主要逻辑
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//测试void menu()//菜单
{printf("###########################################\n");printf("############ 1. 玩游戏 #############\n");printf("############ 2. 退出 #############\n");printf("###########################################\n");
}//游戏函数
void game()
{char ret = 0;char board[ROW][COL] = { 0 };//初始化数组棋盘InitBoard(board, ROW, COL);//打印棋盘DisplayBroad(board, ROW, COL);while (1){//下棋//用户下棋//用户下棋,将所在棋子修改为‘P'PeoplePlay(board, ROW, COL);DisplayBroad(board, ROW, COL);ret = IsWin(board, ROW, COL);if (ret != 'L'){break;}//电脑下棋ComputerPlay(board, ROW, COL);DisplayBroad(board, ROW, COL);//判断是否输赢ret = IsWin(board, ROW, COL);if (ret != 'L'){break;}}if (ret =='P'){printf("用户赢\n");}else if (ret=='C'){printf("电脑赢了\n");}else if (ret == 'Q'){printf("平局!\n");}
}int main()
{srand((unsigned)time(NULL));int input = 0;do{menu();printf("请输入你的选择:>\n");scanf("%d", &input);switch (input){case 1:game();break;case 2:break;default:printf("输入非法,请重新输入!\n");break;}} while (input!=2);return 0;
}
将需要的头文件放入game.h中
game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3//初始化棋盘数组,将全部改为空格
void InitBoard(char board[ROW][COL], int row, int col);//打印棋盘,将棋盘打印出来
void DisplayBroad(char board[ROW][COL], int row, int col);
//用户下棋\
//用户下棋,将所在棋子修改为'P'
void PeoplePlay(char board[ROW][COL], int row, int col);
//电脑下棋
//电脑下棋,将所在棋子修改为'C'
void ComputerPlay(char board[ROW][COL], int row, int col);
//判断是否输赢,如果用户赢,返回'P',电脑赢,返回'C',平局返回'Q',继续为'L'
char IsWin(char board[ROW][COL], int row, int col);
//返回1,表示棋盘满了,0表示还没满
int IsFull(char board[ROW][COL], int row, int col);
将实现游戏的函数封装在game.c中
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"//初始化棋盘数组
void InitBoard(char board[ROW][COL], int row, int col)
{for (int i = 0; i < ROW; i++){for (int j = 0; j < COL; j++){board[i][j] = ' ';}}
}
//打印棋盘
// | |
//---|---|---
// | |
//---|---|---
// | |
void DisplayBroad(char board[ROW][COL], int row, int col)
{for (int i = 0; i < ROW; i++){for (int j = 0; j < COL; j++){printf(" %c ", board[i][j]); if (j<COL-1){printf("|");} } printf("\n");if (i < ROW - 1){for (int j = 0; j < COL ; j++){printf("---");if (j<COL - 1){printf("|");} }printf("\n");} }
}
//用户下棋
//用户下棋,将所在棋子修改为‘P'
void PeoplePlay(char board[ROW][COL], int row, int col)
{printf("玩家下棋:\n");int x = 0, y = 0;while (1){printf("请输入你要下的坐标:>\n");scanf("%d %d", &x, &y);if ((x - 1) >= ROW || (x - 1) < 0 || (y - 1) < 0 || (y - 1) >= COL){printf("输入非法,请重新输入!\n");}else if (board[x-1][y-1] != ' '){printf("该坐标已有棋子!\n");}else{board[x-1][y-1] = 'P';break;} }
}
//电脑下棋
//电脑下棋,将所在棋子修改为'C'
void ComputerPlay(char board[ROW][COL], int row, int col)
{printf("电脑下棋:>\n");while (1){int randx = rand() % ROW;int randy = rand() % COL;if (board[randx][randy] != ' '){continue;}else{board[randx][randy] = 'C';break;}}
}
//判断是否输赢,如果用户赢,返回'P',电脑赢,返回'C',平局返回'Q',继续为'L'int IsFull(char board[ROW][COL], int row, int col)//返回1,表示棋盘满了,0表示还没满
{for (int i = 0; i < ROW; i++){for (int j = 0; j <COL; j++){if (board[i][j]==' ' ){return 0;}}}return 1;
}
char IsWin(char board[ROW][COL], int row, int col)
{//判断横三行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 j = 0; j < COL; j++){if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' '){return board[0][j];}}//斜着if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' '){return board[0][0];}if (board[2][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' '){return board[1][1];}//平局if (1 == IsFull(board, ROW, COL)){return 'Q';}else{return 'L';}
}
演示
几乎将所有的可能都尝试了,结果如下:
这个小游戏中设计很多的知识点,具体想了解更多,请在我的博客中翻找你想具体了解的知识点
Java版本:
棋盘类:
package threeChessGame;import java.util.Arrays;public class Chessboard {private static final int i1 = 0;private static final int p = 1;private static final int a = 2;private final int[][] array = {{i1, i1, i1}, {i1, i1, i1}, {i1, i1, i1}};public boolean moveCircleAt(int row, int column) {if (array[row][column] != i1) {return false;}array[row][column] = p;return true;}public boolean moveCrossAt(int row, int column) {if (array[row][column] != i1) {return false;}array[row][column] = a;return true;}public static final int CIRCLE_WIN = 0; // p赢public static final int CROSS_WIN = 1; // a赢public static final int DRAW = 2; // 平局public static final int CONTINUE = 3; // 游戏继续public int getState() {//行for (int i = 0; i < 3; i++) {if (array[i][0] == array[i][1] && array[i][1] == array[i][2]) {if (array[i][0] == p) {return CIRCLE_WIN;} else if (array[i][0] == a) {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] == p) {return CIRCLE_WIN;} else if (array[0][i] == a) {return CROSS_WIN;}}}//斜线if (array[0][0] == array[1][1] && array[1][1] == array[2][2]) {if (array[1][1] == p) {return CIRCLE_WIN;} else if (array[1][1] == a) {return CROSS_WIN;}}if (array[0][2] == array[1][1] && array[1][1] == array[2][0]) {if (array[1][1] == p) {return CIRCLE_WIN;} else if (array[1][1] == a) {return CROSS_WIN;}}// 没人获胜for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {if (array[i][j] == i1) {return CONTINUE;}}}return DRAW;}private static String show(int i) {switch (i) {case i1:return " ";case p:return "X";case a:return "O";default:return " ";}}@Overridepublic 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 += "-----\n";return s;}public void reset() {for (int i = 0; i < 3; i++) {Arrays.fill(array[i], i1);}}
}
玩家类:
package threeChessGame;import java.util.Scanner;/*** 玩家类*/
public class Player {private final Scanner sc = new Scanner(System.in);public int[] getRowColumn() {System.out.println("输入需要下的位置:[0-2]");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,请重新输入:");}return new int[]{r, c};}public boolean queryContinue() {System.out.println("是否再来一局?true/false");System.out.print(">>>");return sc.nextBoolean();}
}
Ai类
package threeChessGame;import java.util.Random;
/*** Ai类*/
public class AI {private final Random random = new Random();public int[] getRowColumn() {int r = random.nextInt(3);int c = random.nextInt(3);return new int[]{r, c};}
}
游戏类
package threeChessGame;/*** 游戏类*/
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 initialize() {System.out.println("欢迎进入《三子棋》游戏");System.out.println("=====================================");System.out.println(chessboard);}//回合制游戏public void play() {while (true) {if (playerTurn()) {break;}if (aiTurn()) {break;}}}//游戏完成,收尾public void destoroy() {chessboard.reset();}private boolean playerTurn() {System.out.println("玩家下棋:");while (true) {int[] rc = player.getRowColumn();int row = rc[0];int column = rc[1];if (chessboard.moveCircleAt(row, column)) {break;}System.out.println("已有棋子,从新选择!");}System.out.println(chessboard);return chessboard.getState() != chessboard.CONTINUE;}private boolean aiTurn() {System.out.println("Ai下棋:");while (true) {int[] rc = ai.getRowColumn();int row = rc[0];int column = rc[1];if (chessboard.moveCrossAt(row, column)) {break;}}System.out.println(chessboard);return chessboard.getState() != chessboard.CONTINUE;}
}
测试游戏:
package threeChessGame;/*** 三子棋游戏测试*/
public class Test {public static void main(String[] args) {Player player = new Player();Game game = new Game(player);while (true) {//打印棋盘game.initialize();//进行游戏game.play();//结束前处理game.destoroy();//是否继续boolean b = player.queryContinue();if (!b) {System.out.println("欢迎下次游戏!");break;}}}
}
这篇关于三子棋简单小游戏的详细的实现(C语言和java两种)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!