#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;
const int N = 15; //15*15的棋盘
const char ChessBoard = ' '; //棋盘标志
const char flag1 = 'o'; //玩家1或电脑标志
const char flag2 = 'x'; //玩家2标志
typedef struct Position{ //坐标
int row; //行
int col; //列
}Position;
class GoBang{ //五子棋类
public:
GoBang(){
InitChessBoard(); //初始化棋盘
}
void Play(){ //下棋
Position Play1; //玩家1或电脑
Position Play2; //玩家2
while (1){
int mode = ChoiceMode();
while (1){
if (mode == 1){ //电脑VS玩家
ComputerChess(Play1, flag1); //电脑走
if (GetVictory(Play1, 0, flag1)){ //0代表电脑,为真则表示电脑获胜
break;
}
PlayChess(Play2, 2, flag2); //玩家2走
if (GetVictory(Play2, 2, flag2)){ //2代表玩家2
break;
}
}
else{ //玩家1VS玩家2
PlayChess(Play1, 1, flag1); //玩家1走
if (GetVictory(Play1, 1, flag1)){ //玩家1赢
break;
}
PlayChess(Play2, 2, flag2); //玩家2走
if (GetVictory(Play2, 2, flag2)){ //玩家2赢
break;
}
}
}
cout << "======再来一局=======" << endl;
cout << "yes or no :";
char s[] = "yes";
cin >> s;
if (strcmp(s, "no") == 0){
break;
}
}
}
protected:
void InitChessBoard(){ //初始化棋盘
for (int i = 0; i < N + 1; ++i){
for (int j = 0; j < N + 1; ++j){
_ChessBoard[i][j] = ChessBoard;
}
}
}
int ChoiceMode(){ //选择模式
system("cls");
//系统调用,清屏
InitChessBoard(); //重新初始化棋盘
cout << "
$" << endl;
cout << "
\033[31m0、退出\033[0m
\0
33
[
31
𝑚
0
、
退
出
\0
33
[
0
𝑚
"<<endl;cout<<"
"<<
𝑒
𝑛
𝑑
𝑙
;
𝑐
𝑜
𝑢
𝑡
<<"
\033[33m1、电脑VS玩家\033[0m
"<<endl;cout<<"
"<<
𝑒
𝑛
𝑑
𝑙
;
𝑐
𝑜
𝑢
𝑡
<<"
\033[32m2、玩家VS玩家\033[0m
"<<endl;cout<<"
"<<
𝑒
𝑛
𝑑
𝑙
;
𝑐
𝑜
𝑢
𝑡
<<"
$$$" << endl;
while (1){
int i = 0;
cout << "\033[34m请选择模式:\033[0m";
cin >> i;
if (i == 0){ //退出
exit(1);
}
if (i == 1 || i == 2){
return i;
}
else{
cout << "\033[31m非法输入,请重新输入!\033[0m" << endl;
}
}
}
void PrintChessBoard(){ //打印棋盘
printf(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n");
printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
for (int i = 1; i < N + 1; ++i)
{
printf("%2d ", i);
printf("| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", _ChessBoard[i][1], _ChessBoard[i][2], _ChessBoard[i][3], _ChessBoard[i][4], _ChessBoard[i][5], _ChessBoard[i][6], _ChessBoard[i][7], _ChessBoard[i][8], _ChessBoard[i][9], _ChessBoard[i][10], _ChessBoard[i][11], _ChessBoard[i][12], _ChessBoard[i][13], _ChessBoard[i][14], _ChessBoard[i][15]);
printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
}
cout << endl;
}
void ComputerChess(Position& pos, char flag){ //电脑走
//PrintChessBoard(); //打印棋盘
int x = 0;
int y = 0;
while (1){ //循环查找空位置
x = (rand() % N) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
y = (rand() % N) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
if (_ChessBoard[x][y] == ChessBoard){ //如果这个位置为空(没有棋子),跳出循环,下棋
break;
}
}
pos.row = x;
pos.col = y;
_ChessBoard[pos.row][pos.col] = flag;
}
void PlayChess(Position& pos, int player, char flag){
PrintChessBoard(); //打印棋盘
while (1){
printf("请玩家%d输入坐标:", player);
cin >> pos.row >> pos.col;
if (JudgeValue(pos) == 1){ //判断坐标是否合法
break;
}
cout << "坐标不合法,请重新输入:" << endl;
}
_ChessBoard[pos.row][pos.col] = flag;
}
int JudgeValue(const Position& pos){ //判断坐标的合法性
//1.在棋盘上
if (pos.row > 0 && pos.row <= N && pos.col > 0 && pos.col <= N){
//2.所在位置为空(没有棋子)
if (_ChessBoard[pos.row][pos.col] == ChessBoard){
return 1; //合法
}
}
return 0; //非法
}
int JudgeVictory(Position pos, char flag){ //判断是否有玩家获胜(底层判断)
int begin = 0;
int end = 0;
//1.判断行是否满足条件
(pos.col - 4) > 0 ? begin = (pos.col - 4) : begin = 1;
(pos.col + 4) > N ? end = N : end = (pos.col + 4);
for (int i = pos.row, j = begin; j + 4 <= end; ++j){
if (_ChessBoard[i][j] == flag && _ChessBoard[i][j + 1] == flag &&
_ChessBoard[i][j + 2] == flag && _ChessBoard[i][j + 3] == flag &&
_ChessBoard[i][j + 4] == flag)
return 1;
}
//2.判断列是否满足条件
(pos.row - 4) > 0 ? begin = (pos.row - 4) : begin = 1;
(pos.row + 4) > N ? end = N : end = (pos.row + 4);
for (int j = pos.col, i = begin ; i + 4 <= end; ++i){
if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j] == flag &&
_ChessBoard[i + 2][j] == flag && _ChessBoard[i + 3][j] == flag &&
_ChessBoard[i + 4][j] == flag)
return 1;
}
//3.判断主对角线是否满足条件
int len = 0; //相对长度
int start = 0;
int finish = 0;
pos.row > pos.col ? len = pos.col - 1 : len = pos.row - 1;
if (len > 4){
len = 4;
}
begin = pos.row - len; //横坐标起始位置
start = pos.col - len; //纵坐标起始位置
pos.row > pos.col ? len = N - pos.row : len = N - pos.col;
if (len > 4){
len = 4;
}
end = pos.row + len; //横坐标结束位置
finish = pos.col + len; //纵坐标结束位置
for (int i = begin, j = start; (i + 4 <= end) && (j + 4 <= finish); ++i, ++j){
if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j + 1] == flag &&
_ChessBoard[i + 2][j + 2] == flag && _ChessBoard[i + 3][j + 3] == flag &&
_ChessBoard[i + 4][j + 4] == flag)
return 1;
}
//4.判断副对角线是否满足条件
(pos.row - 1) > (N - pos.col) ? len = N - pos.col : len = pos.row - 1;
if (len > 4){
len = 4;
}
begin = pos.row - len; //横坐标起始位置
start = pos.col + len; //纵坐标起始位置
(N - pos.row) > (pos.col - 1) ? len = pos.col - 1 : len = N - pos.row;
if (len > 4){
len = 4;
}
end = pos.row + len; //横坐标结束位置
finish = pos.col - len; //纵坐标结束位置
for (int i = begin, j = start; (i + 4 <= end) && (j - 4 >= finish); ++i, --j){
if (_ChessBoard[i][j] == flag && _ChessBoard[i + 1][j - 1] == flag &&
_ChessBoard[i + 2][j - 2] == flag && _ChessBoard[i + 3][j - 3] == flag &&
_ChessBoard[i + 4][j - 4] == flag)
return 1;
}
//该位置并未下棋
for (int x = 1; x < N + 1; ++x){
for (int y = 1; y < N + 1; ++y){
if (_ChessBoard[x][y] == ChessBoard){
return 0; //未下棋
}
}
}
return -1; //和局
}
bool GetVictory(Position& pos, int player, char flag){ //判断具体哪位玩家赢
if (JudgeVictory(pos, flag) != 0){ //判断有无人获胜
if (JudgeVictory(pos, flag) == 1){ //判断是否有人获胜,1表示获胜
PrintChessBoard(); //打印棋盘
if (player == 0){
cout << "电脑获胜!" << endl;
}
else{
printf("恭喜玩家%d获胜!\n", player);
}
}
else{
printf("和局!\n");
}
return true; //有人获胜
}
return false; //没人获胜
}
private:
char _ChessBoard[N + 1][N + 1]; //棋盘
};
int main(){
GoBang g;
g.Play();
system("pause");
return 0;
}
井字棋:
先是不好用的
#include<iostream>
using namespace std;
//bool t=0;
char x[3][3];
short t=0;
int Winning_X()
{
if(x[1][1]=='X'&&x[2][1]=='X'&&x[3][1]=='X')
{
//X| |
//_|_|_
//X| |
//_|_|_
//X| |
t=1;
//return;
}
if(x[1][1]=='X'&&x[1][2]=='X'&&x[1][3]=='X')
{
//X|X|X
//_|_|_
// | |
//_|_|_
// | |
t=1;
//return;
}
if(x[1][1]=='X'&&x[2][2]=='X'&&x[3][3]=='X')
{
//X| |
//_|_|_
// |X|
//_|_|_
// | |X
t=1;
//return;
}
if(x[1][3]=='X'&&x[2][2]=='X'&&x[3][1]=='X')
{
// | |X
//_|_|_
// |X|
//_|_|_
//X| |
t=1;
//return;
}
if(x[1][1]=='X'&&x[2][2]=='X'&&x[3][3]=='X')
{
// |X|
//_|_|_
// |X|
//_|_|_
// |X|
t=1;
//return;
}
if(x[1][3]=='X'&&x[2][3]=='X'&&x[3][3]=='X')
{
// | |X
//_|_|_
// | |X
//_|_|_
// | |X
t=1;
//return;
}
if(x[1][1]=='X'&&x[1][2]=='X'&&x[1][3]=='X')
{
//X|X|X
//_|_|_
// | |
//_|_|_
// | |
t=1;
//return;
}
if(x[2][1]=='X'&&x[2][2]=='X'&&x[2][3]=='X')
{
// | |
//_|_|_
//X|X|X
//_|_|_
// | |
t=1;
//return;
}
if(x[3][1]=='X'&&x[3][2]=='X'&&x[3][3]=='X')
{
// | |
//_|_|_
// | |
//_|_|_
//X|X|X
t=1;
//return;
}
}
int Winning_O()
{
if(x[1][1]=='O'&&x[2][1]=='O'&&x[3][1]=='O')
{
//O| |
//_|_|_
//O| |
//_|_|_
//O| |
t=1;
t++;
//return;
}
if(x[1][1]=='O'&&x[1][2]=='O'&&x[1][3]=='O')
{
//O|O|O
//_|_|_
// | |
//_|_|_
// | |
t=1;
t++;
//return;
}
if(x[1][1]=='O'&&x[2][2]=='O'&&x[3][3]=='O')
{
//O| |
//_|_|_
// |O|
//_|_|_
// | |O
t=1;
t++;
//return;
}
if(x[1][3]=='O'&&x[2][2]=='O'&&x[3][1]=='O')
{
// | |O
//_|_|_
// |O|
//_|_|_
//O| |
t=1;
t++;
//return;
}
if(x[1][1]=='O'&&x[2][2]=='O'&&x[3][3]=='O')
{
// |O|
//_|_|_
// |O|
//_|_|_
// |O|
t=1;
t++;
//return;
}
if(x[1][3]=='O'&&x[2][3]=='O'&&x[3][3]=='O')
{
// | |O
//_|_|_
// | |O
//_|_|_
// | |O
t=1;
t++;
//return;
}
if(x[1][1]=='O'&&x[1][2]=='O'&&x[1][3]=='O')
{
//X|X|X
//_|_|_
// | |
//_|_|_
// | |
t=1;
t++;
//return;
}
if(x[2][1]=='O'&&x[2][2]=='O'&&x[2][3]=='O')
{
// | |
//_|_|_
//O|O|O
//_|_|_
// | |
t=1;
t++;
//return;
}
if(x[3][1]=='O'&&x[3][2]=='O'&&x[3][3]=='O')
{
// | |
//_|_|_
// | |
//_|_|_
//O|O|O
t=1;
t++;
//return;
}
}
void play_X()
{
int x1,y1;
cout<<"锟斤拷锟斤拷锟斤拷 X 锟斤拷业锟斤拷锟斤拷锟斤拷锟斤拷锟絓nPlease enter the coordinates of player X's chess:";
cin>>x1>>y1;
if(x[x1][y1]!=' ')
{
cout<<"锟斤拷锟斤拷胤锟斤拷丫锟斤拷锟斤拷锟斤拷锟斤拷耍锟絋here are already chess pieces in this place!";
system("pause");
play_X();
}
else
{
x[x1][y1]='X';
}
}
void play_O()
{
int x2,y2;
cout<<"锟斤拷锟斤拷锟斤拷 O 锟斤拷业锟斤拷锟斤拷锟斤拷锟斤拷锟絓nPlease enter the coordinates of player O's chess:";
cin>>x2>>y2;
if(x[x2][y2]!=' ')
{
cout<<"锟斤拷锟斤拷胤锟斤拷丫锟斤拷锟斤拷锟斤拷锟斤拷耍锟絋here are already chess pieces in this place!";
system("pause");
play_O();
}
else
{
x[x2][y2]='O';
}
}
void initialize()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
x[i][j]=' ';
}
}
t=0;
}
void print()
{
system("cls");
cout<<"\033[46mP L A Y T H E G A M E\n\033[0m";
cout<<"\033[6m锟斤拷锟斤拷锟秸筹拷锟斤拷锟斤拷\n chen_zi_yi\033[6m";
cout<<"\n \033[33m锟斤拷锟斤拷锟斤拷\n\033[0m";
cout<<"\033[31mTic-Tac-Toe\n\033[0m";
cout<<x[1][1]<<"\033[45m|\033[0m"<<x[1][2]<<"\033[45m|\033[0m"<<x[1][3]<<endl;
cout<<"\033[45m_|_|_\n\033[0m";
cout<<x[2][1]<<"\033[45m|\033[0m"<<x[2][2]<<"\033[45m|\033[0m"<<x[2][3]<<endl;
cout<<"\033[45m_|_|_\n\033[0m";
cout<<x[3][1]<<"\033[45m|\033[0m"<<x[3][2]<<"\033[45m|\033[0m"<<x[3][3]<<endl;
// | |
//_|_|_
// | |
//_|_|_
// | |
}
void win_X()
{
cout<<"\033[33mX锟斤拷锟绞わ拷锟斤拷锟絇layer X wins!\n\033[0m";
}
void win_O()
{
cout<<"\033[31mO锟斤拷锟绞わ拷锟斤拷锟絇layer O wins!\033[0m";
}
int main()
{
initialize();
print();
while(!t)
{
print();
play_X();
print();
play_O();
print();
Winning_X();
Winning_O();
}
if(t==1)
{
win_X();
}
else
{
win_O();
}
return 0;
}
呃.......中文识别不了,自己改
好用的
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
class TicTacToe
{
private:
char currentPlayer;
char board[3][3];
public:
TicTacToe();
void print();
char getCurrentPlayer();
char getWinner();
bool isDone();
bool isValidMove(int row,int col);
void makeMove(int row,int col);
void makeAutoMove();
};
TicTacToe::TicTacToe(){
currentPlayer = 'X';
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
board[i][j] = '-';
}
}
}
void TicTacToe::print(){
cout<<" 1 2 3"<<endl;cout<<endl;
for(int i=0;i<3;i++){
cout<<i+1<<" ";
for(int j=0;j<3;j++){
cout<<board[i][j]<<" ";
}
cout<<endl;cout<<endl;
}
}
char TicTacToe::getCurrentPlayer(){
return currentPlayer;
}
char TicTacToe::getWinner(){
//返回赢家信息(’X’ 或 ’O’),如果没有赢家,返回 ’-’
for(int i=0;i<3;i++){
//三棋子横成一排
if(board[i][0] != '-' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
return board[i][0];
//三棋子竖成一排
if(board[0][i] != '-' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
return board[0][i];
}
//三棋子斜成一排
if((board[1][1] != '-') && (
(board[0][0] == board[1][1] && board[1][1] == board[2][2])||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]) )
){
return board[1][1];
}else{
return '-';
}
}
bool TicTacToe::isDone(){
if(getWinner() != '-'){
return true;
}else{
return false;
}
}
bool TicTacToe::isValidMove(int row,int col){
//检查行列值是否在 1-3 之间,如果该位置可用,返回ture,否则返回 false
if( row>=1 && row<=3 && col>=1 && col <=3 ){
return true;
}else{
return false;
}
}
void TicTacToe::makeMove(int row,int col){
//将指定位置设置为玩家的符号,切换玩家
if(isValidMove(row,col)){
if( board[row-1][col-1] == '-' ){
board[row-1][col-1] = currentPlayer;
if(currentPlayer == 'X'){
currentPlayer = 'O';
}else{
currentPlayer = 'X';
}
}else{
cout<<"该位置已被占用!"<<endl;
}
}else{
cout<<"输入的位置无效"<<endl;
}
}
void TicTacToe::makeAutoMove(){
srand(int(time(0)));
label:
int row = rand()%3+1;
int col = rand()%3+1;
if( board[row-1][col-1] == '-' ){
board[row-1][col-1] = currentPlayer;
if(currentPlayer == 'X'){
currentPlayer = 'O';
}else{
currentPlayer = 'X';
}
}else{
goto label;
}
row = rand()%3+1;
col = rand()%3+1;
}
void testTicTacToe(){
cout<<"您是否想开始游戏?(y/n)"<<endl;
char conti;
int mode;
cin>>conti;
TicTacToe tic;
while(conti == 'y'){
cout<<"请选择单人模式或者双人模式?(1/2)"<<endl;
cin>>mode;
if(mode == 1 || mode == 2){
if(mode == 1){
tic.print();
int row,col;
bool flag = true; //表征当前玩家为用户或计算机
while(!tic.isDone()){
if(flag){ //当前玩家为用户
cout<<"当前玩家为:"<<tic.getCurrentPlayer()<<endl;
cout<<"请按坐标输入您的走步:";
cin>>row>>col;
if(tic.getCurrentPlayer() == 'X'){
tic.makeMove(row,col);
}else{
tic.makeAutoMove();
}
flag = false;
}else{ //当前玩家为计算机
cout<<"当前玩家为:"<<tic.getCurrentPlayer()<<endl;
tic.makeAutoMove();
flag = true;
}
tic.print();
}
}else{ //mode == 2
tic.print();
int row,col;
while(!tic.isDone()){
cout<<"当前玩家为:"<<tic.getCurrentPlayer()<<endl;
cout<<"请按坐标输入您的走步:";
cin>>row>>col;
tic.makeMove(row,col);
tic.print();
}
}
cout<<"本局结束,胜者为:"<<tic.getWinner()<<endl;
}else{
cout<<"请输入正确的模式代码"<<endl;
}
cout<<"您是否想继续游戏?(y/n)"<<endl;
cin>>conti;
}
}
int main(){
testTicTacToe();
return 0;
}
这个中文没问题了。
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/s2015_818/article/details/141819485