三子棋
Posted 雨轩(小宇)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三子棋相关的知识,希望对你有一定的参考价值。
三子棋实现(c/c++)
c/c++版的三子棋
直接上代码
下面展示一些 具体代码
。
// 类的声明
// An highlighted block
#pragma once
#include<iostream>
using namespace std;
class MyClass
{
public:
// 游戏开始页面
void game();
};
// 实现方法展示
// An highlighted block
#include"game.h"
#define ROW 3 //行列
#define COL 3
// 1.初始化棋盘
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 DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << board[i][j] ;
if (j < col - 1)
cout << " |";
}
cout << endl;
if (i < row - 1)
{
for (int j = 0; j < col; j++)
{
cout << "---";
if (j < col - 1)
cout << "|";
}
cout << endl;
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
cout << "玩家走:>";
while (1)
{
cout << "请输入要下的坐标";
cin >> x;
cin >> y;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
cout << "该坐标被占用";
}
}
else
{
cout << "坐标非法,请重新输入";
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
cout << "电脑走:>" << endl;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
// 返回1表示棋盘满,返回0表示没满
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (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][1] != ' ')
{
return board[i][1];
}
}
for (int i = 0; i < col; i++)
{
// 纵向判断
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[1][i];
}
}
// 斜线也就是交叉线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][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';
}
return 'C';
}
void MyClass::game()
{
char win = ' ';
char board[ROW][COL] = { 0 };
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1)
{
PlayerMove(board,ROW,COL);
DisplayBoard(board, ROW, COL);
//判断选手是否胜出
win = IsWin(board, ROW, COL);
if (win != 'C')
{
break;
}
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
win = IsWin(board, ROW, COL);
if (win != 'C')
{
break;
}
}
if (win == '*')
{
cout << "玩家胜!" << endl;
}
else if (win == '#')
{
cout << "电脑胜" << endl;
}
else
{
cout << "平局" << endl;
}
}
// 主函数
// An highlighted block
#include"game.h"
void menu()
{
cout << "**************" << endl;
cout << "****三子棋****" << endl;
cout << "**************" << endl;
}
void test()
{
int input = 0;
do
{
menu();
cout << "请选择: 1.开始三子棋游戏 0.退出游戏>" << endl;
cin >> input;
switch (input)
{
case 1: cout << "三子棋游戏" << endl; MyClass Game; Game.game(); break;
case 0: cout << "退出游戏" << endl; break;
default:cout << "选择错误,退出游戏!" << endl; break;
}
} while (input);
}
int main()
{
test();
}
以上是关于三子棋的主要内容,如果未能解决你的问题,请参考以下文章