棋类游戏大全
2025-12-08 23:08:21
发布于:四川
加入我们看全部代码!!!#大阴帝国
代码如下:
//棋类游戏大全
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
const int N = 15;
int nx,ny;
int x_,y_;
string name1,name2;
char oc = '.';
bool color = 1;
char table[N][N];
bool ff;
char f(int x,int y){
char co = table[x][y];
if(co=='.'){
ff = 0;
return '.';
}int cnt=0;
for(int i=y;i<=y+4&&i<14;i++)if(table[x][i] == co)cnt++;
if(cnt==5){
ff = 1;
return co;
}cnt = 0;
for(int i=x;i<=x+4&&i<14;i++)if(table[i][y] == co)cnt++;
if(cnt==5){
ff = 1;
return co;
}cnt = 0;
for(int i=0;i<=4;i++)if(table[x+i][y+i]==co and x+i<14 and y+i<14)cnt++;
if(cnt==5){
ff = 1;
return co;
}cnt = 0;
for(int i=0;i<=4;i++)if(table[x-i][y+i]==co and x-i>0 and y+i<14)cnt++;
if(cnt==5){
ff = 1;
return co;
}ff=0;
return co;
}bool dw(){
for(int i=1;i<=13;i++){
for(int j=1;j<=13;j++){
char dd= f(i,j);
if(ff){
if(dd=='O')cout << "\nPlayer1:" << name1 <<"赢了\n";
else cout <<"\nPlayer2:" << name2 << "赢了\n";
cout<<"\n按下任意键继续......";
getch();
system("cls");
return false;
}
}
}return true;
}
void cgoto(short x,short y){
COORD gxy = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, gxy);
}void init(){
color = 1;
nx = ny = 6;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(i==nx and j==ny)table[i][j] = '+';
else if(!i or !j or i==N-1 or j==N-1)table[i][j] = '#';
else table[i][j] = '.';
}
}
}void print(){
cgoto(0,0);
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cout << table[i][j] << " ";
if(table[i][j]=='+'||table[i][j]=='!'){
x_=j;
y_=i;
}
}cout << endl;
}cout<<"#("<<x_<<","<<y_<<")";
cout<<"\nPlayer1: "<<name1<<",棋子形状为O"<<"\nPlayer2:"<<name2<<",棋子形状为X"<<"\n'+'为落子点"<<" '!'为不可落子点";
}void move(int px,int py){
table[nx][ny] = oc;
oc = '.';
nx += px,ny+=py;
if(nx<1 or ny<1 or nx>13 or ny>13)nx -=px,ny-=py;
if(table[nx][ny]!='.'){
oc = table[nx][ny];
table[nx][ny] = '!';
}
else table[nx][ny] = '+';
}void down(){
if(table[nx][ny]!='+')return;
if(color){
oc = 'O';
table[nx][ny] = 'O';
}else{
table[nx][ny] = 'X';
oc = 'X';
}color = !color;
}
void cout_string_ong_xgy(string s){
int len=s.size();
for(int i=0;i<len;i++){
cout<<s[i];
Sleep(10);
}Sleep(2000);
system("cls");
}
void Gobang(){
cout_string_ong_xgy("xx xx ggggg yy yy\n x x g y y\n x g ggg y\n x x g g y\nxx xx ggggg y\n\n请稍后正在处理中......");
cout<<"# # # # # # # # # # #\n# #\n# # 五子棋\n# #\n# # 小游戏\n# #\n# #\n# #\n# #\n# #\n# # # # # # # # # # #\n按下任意键继续......";
getch();
system("cls");
string s;
int len=0;
bool end = 1;
cout << "请输入Player1的名字,他的棋子形状是O"<<endl;
cin >> name1;
cout << "请输入Player2的名字,他的棋子形状是X"<<endl;
cin >> name2;
system("cls");
while(end){
init();
print();
oc = '.';
while(dw()){
char f;
if(GetAsyncKeyState(0x25) & 0x8000){move(0,-1);print();Sleep(500);}
if(GetAsyncKeyState(0x26) & 0x8000){move(-1,0);print();Sleep(500);}
if(GetAsyncKeyState(0x27) & 0x8000){move(0,1);print();Sleep(500);}
if(GetAsyncKeyState(0x28) & 0x8000){move(1,0);print();Sleep(500);}
f = getch();
if(f=='a'){
move(0,-1);print();
}
if(f=='s'){
move(1,0);print();
}
if(f=='d'){
move(0,1);print();
}
if(f=='w'){
move(-1,0);print();
}
if(f==' '){
down();print();
}
}while(1){
cout << "按下c键进行下一轮e键结束\n";
char input = getch();
if(input=='c')break;
else if(input=='e'){
end = 0;
break;
}else MessageBox(NULL,"输入错误","提示",MB_OK|MB_ICONHAND);
}
}
}
// 棋盘大小(井字棋为3x3)
const int BOARD_SIZE = 3;
// 初始化棋盘,用空格表示空位置
char board[BOARD_SIZE][BOARD_SIZE] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
// 绘制棋盘
void drawBoard() {
cout << " 1 2 3" << endl; // 列号
cout << "1 " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << " --+---+--" << endl;
cout << "2 " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << " --+---+--" << endl;
cout << "3 " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
cout << endl;
}
// 检查是否获胜
bool checkWin(char player) {
// 检查行
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
}
// 检查列
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
return true;
}
}
// 检查对角线
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
// 检查棋盘是否已满(平局)
bool checkDraw() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == ' ') { // 还有空位置
return false;
}
}
}
return true;
}
// 玩家落子
void playerMove() {
int row, col;
while (true) {
cout << "请输入你要落子的位置(行 列,如 1 2):";
cin >> row >> col;
// 转换为数组索引(用户输入1-3,数组是0-2)
row--;
col--;
// 检查输入是否合法
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
cout << "输入无效!行和列必须是 1-3 之间的数字。" << endl;
} else if (board[row][col] != ' ') {
cout << "该位置已被占用!请重新选择。" << endl;
} else {
board[row][col] = 'X'; // 玩家使用 X
break;
}
}
}
// 电脑落子(简单随机策略)
void computerMove() {
cout << "电脑落子中..." << endl;
srand(time(0)); // 初始化随机数种子
int row, col;
while (true) {
// 随机生成 0-2 的行和列
row = rand() % BOARD_SIZE;
col = rand() % BOARD_SIZE;
// 检查位置是否为空
if (board[row][col] == ' ') {
board[row][col] = 'O'; // 电脑使用 O
break;
}
}
}
void Tic_Tac_Toe(){
cout_string_ong_xgy("xx xx ggggg yy yy\n x x g y y\n x g ggg y\n x x g g y\nxx xx ggggg y\n\n请稍后正在处理中......");
//标记
int end = 1;
while (1) {
cout << "=== 井字棋游戏 ===" << endl;
cout << "玩家:X | 电脑:O" << endl << endl;
// 绘制棋盘
drawBoard();
// 玩家落子
playerMove();
// 检查玩家是否获胜
if (checkWin('X')) {
drawBoard();
cout << "恭喜你!你赢了!" << endl;
end = 2;
}
// 检查是否平局
if (checkDraw()) {
drawBoard();
cout << "平局!棋盘已满,无人获胜。" << endl;
end = 2;
}
// 电脑落子
computerMove();
// 检查电脑是否获胜
if (checkWin('O')) {
drawBoard();
cout << "很遗憾!电脑赢了。" << endl;
end = 2;
}
// 检查是否平局
if (checkDraw()) {
drawBoard();
cout << "平局!棋盘已满,无人获胜。" << endl;
end = 2;
}
if (end == 2) {
cout << "胜负已分!3S后结算。";
Sleep(3000);
for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
board[i][j]=' ';
}
}
system("cls");
//询问是否退出
while (1) {
cout << "按下c键进行下一轮e键结束\n";
char input = getchar();
if (input == 'c') {
system("cls");
break;
}else if (input == 'e') {
end = 0;
return ;
} else MessageBox(NULL,"输入错误","提示",MB_OK|MB_ICONHAND);
}
} else {
system("cls");
}
}for(int i=0;i<=2;i++){
for(int j=0;j<=2;j++){
board[i][j]=' ';
}
}
return ;
}
char c='X';
// 棋盘大小(井字棋为3x3)
const int B = 3;
// 初始化棋盘,用空格表示空位置
char b[B][B] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
// 绘制棋盘
void drawB() {
cout << " 1 2 3" << endl; // 列号
cout << "1 " << b[0][0] << " | " << b[0][1] << " | " << b[0][2] << endl;
cout << " --+---+--" << endl;
cout << "2 " << b[1][0] << " | " << b[1][1] << " | " << b[1][2] << endl;
cout << " --+---+--" << endl;
cout << "3 " << b[2][0] << " | " << b[2][1] << " | " << b[2][2] << endl;
cout << endl;
}
// 检查是否获胜
bool Win(char player) {
// 检查行
for (int i = 0; i < 3; i++) {
if (b[i][0] == player && b[i][1] == player && b[i][2] == player) {
return true;
}
}
// 检查列
for (int j = 0; j < 3; j++) {
if (b[0][j] == player && b[1][j] == player && b[2][j] == player) {
return true;
}
}
// 检查对角线
if (b[0][0] == player && b[1][1] == player && b[2][2] == player) {
return true;
}
if (b[0][2] == player && b[1][1] == player && b[2][0] == player) {
return true;
}
return false;
}
// 检查棋盘是否已满(平局)
bool cD() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (b[i][j] == ' ') { // 还有空位置
return false;
}
}
}
return true;
}
// 玩家落子
void playerM() {
int row, col;
while (true) {
if(c=='X')cout << "玩家1:请输入你要落子的位置(行 列,如 1 2):";
else if(c=='O') cout << "玩家2:请输入你要落子的位置(行 列,如 1 2):";
cin >> row >> col;
// 转换为数组索引(用户输入1-3,数组是0-2)
row--;
col--;
// 检查输入是否合法
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
cout << "输入无效!行和列必须是 1-3 之间的数字。" << endl;
} else if (b[row][col] != ' ') {
cout << "该位置已被占用!请重新选择。" << endl;
} else {
b[row][col] = c;
break;
}
}
if(c=='O'){
c='X';
}else{
c='O';
}
}
void Tic_Tac_Toe_1(){
//cout << "=== 井字棋游戏 ===" << endl;
//cout << "玩家:X | 电脑:O" << endl << endl;
cout_string_ong_xgy("xx xx ggggg yy yy\n x x g y y\n x g ggg y\n x x g g y\nxx xx ggggg y\n\n请稍后正在处理中......");
//标记
int end = 1;
while (1) {
cout << "=== 井字棋游戏 ===" << endl;
cout << "玩家1:X | 玩家2:O" << endl << endl;
// 绘制棋盘
drawB();
// 玩家落子
playerM();
// 检查玩家是否获胜
if (Win('X')) {
drawB();
cout << "恭喜玩家1,赢得比赛!" << endl;
end = 2;
}
// 检查是否平局
if (cD()) {
drawB();
cout << "平局!棋盘已满,无人获胜。" << endl;
end = 2;
}
if (end == 2) {
cout << "胜负已分!3S后结算。";
Sleep(3000);
system("cls");
//询问是否退出
while (1) {
cout << "按下c键进行下一轮e键结束\n";
char input = getchar();
if (input == 'c') {
system("cls");
break;
}else if (input == 'e') {
end = 0;
return ;
} else cout << "输入错误,请重新输入\n";
}
} else {
system("cls");
}
system("cls");
cout << "=== 井字棋游戏 ===" << endl;
cout << "玩家1:X | 玩家2:O" << endl << endl;
// 绘制棋盘
drawB();
// 玩家落子
playerMove();
// 检查是否获胜
if (Win('O')) {
drawB();
cout << "恭喜玩家2,赢得比赛!" << endl;
end = 2;
}
// 检查是否平局
if (cD()) {
drawB();
cout << "平局!棋盘已满,无人获胜。" << endl;
end = 2;
}
if (end == 2) {
cout << "胜负已分!3S后结算。";
Sleep(3000);
system("cls");
//询问是否退出
while (1) {
cout << "按下c键进行下一轮e键结束\n";
char input = getchar();
if (input == 'c') {
system("cls");
break;
}else if (input == 'e') {
end = 0;
return ;
} else MessageBox(NULL,"输入错误","提示",MB_OK|MB_ICONHAND);
}
} else {
system("cls");
}
}
return ;
}
这里空空如也












有帮助,赞一个