c++井字棋

#include<bits/stdc++.h>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<unistd.h>
#include <windows.h>
using namespace std;
//	前景色:数字0-15
//	前景加强:数字8
//	背景色:数字16 32 64
//	背景加强: 数字128
//  0=黑色                8=灰色  
//   1=蓝色                9=淡蓝色    
//  2=绿色                10=淡绿色
//  3=湖蓝色              11=淡浅绿色
//  4=红色                12=淡红色
//  5=紫色                13=淡紫色
//  6=黄色                14=淡黄色
void C(short x) {
	if(x>=0 && x<=15) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
int main() {
	int sr;
	char a[3][3];
	for(int i=0; i<3; i++) {
		for(int j=0; j<3; j++) {
			a[i][j]='+';
		}
	}
	bool f=0;
	while(!f) {
		for(int i=0; i<3; i++) {
			for(int j=0; j<3; j++) {
				if(a[i][j]=='*') {
					C(4);
				} else if(a[i][j]=='/') {
					C(9);
				}
				cout<<a[i][j]<<" ";
				C(15);
			}
			cout<<"\n";
		}
		int x,y;
		cout<<"请输入你要下棋的位置(i行j列):\n";
		cout<<"i:";
		cin>>x;
		cout<<"j:";
		cin>>y;
		x--,y--;
		while(a[x][y]=='*'||a[x][y]=='/') {
			cout<<"这个地方已经下过了,请重新输入:\n";
			cout<<"i:";
			cin>>x;
			cout<<"j:";
			cin>>y;
		}
		a[x][y]='*';
		system("cls");
		// 0 1 2
		//0* * *
		//1* * *
		//2* * *
		if(a[0][0]=='*'&&a[0][1]=='*'&&a[0][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		if(a[1][0]=='*'&&a[1][1]=='*'&&a[1][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		if(a[2][0]=='*'&&a[2][1]=='*'&&a[2][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		//61~63 行
		if(a[0][0]=='*'&&a[1][0]=='*'&&a[2][0]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		if(a[0][1]=='*'&&a[1][1]=='*'&&a[2][1]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		if(a[0][2]=='*'&&a[1][2]=='*'&&a[2][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		//65~67 列
		if(a[0][0]=='*'&&a[1][1]=='*'&&a[2][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		if(a[0][2]=='*'&&a[1][1]=='*'&&a[2][0]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
		}
		//69~70 斜
		//61~71 Win
		if(a[0][0]=='/'&&a[0][1]=='/'&&a[0][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[1][0]=='/'&&a[1][1]=='/'&&a[1][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[2][0]=='/'&&a[2][1]=='/'&&a[2][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		//73~75 行
		if(a[0][0]=='/'&&a[1][0]=='/'&&a[2][0]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[0][1]=='/'&&a[1][1]=='/'&&a[2][1]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[0][2]=='/'&&a[1][2]=='/'&&a[2][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		//77~79 列
		if(a[0][0]=='/'&&a[1][1]=='/'&&a[2][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[0][2]=='/'&&a[1][1]=='/'&&a[2][0]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		//81~82 斜
		//73~83 Lose
		if( (a[0][0]=='*'&&a[0][1]=='*'&&a[0][2]=='*')
		        ||  (a[1][0]=='*'&&a[1][1]=='*'&&a[1][2]=='*')
		        ||  (a[2][0]=='*'&&a[2][1]=='*'&&a[2][2]=='*')
		        ||  (a[0][0]=='*'&&a[1][0]=='*'&&a[2][0]=='*')
		        ||  (a[0][1]=='*'&&a[1][1]=='*'&&a[2][1]=='*')
		        ||  (a[0][2]=='*'&&a[1][2]=='*'&&a[2][2]=='*')
		        ||  (a[0][0]=='*'&&a[1][1]=='*'&&a[2][2]=='*')
		        ||  (a[0][2]=='*'&&a[1][1]=='*'&&a[2][0]=='*')
		        ||  (a[0][0]=='/'&&a[0][1]=='/'&&a[0][2]=='/')
		        ||  (a[1][0]=='/'&&a[1][1]=='/'&&a[1][2]=='/')
		        ||  (a[2][0]=='/'&&a[2][1]=='/'&&a[2][2]=='/')
		        ||  (a[0][0]=='/'&&a[1][0]=='/'&&a[2][0]=='/')
		        ||  (a[0][1]=='/'&&a[1][1]=='/'&&a[2][1]=='/')
		        ||  (a[0][2]=='/'&&a[1][2]=='/'&&a[2][2]=='/')
		        ||  (a[0][0]=='/'&&a[1][1]=='/'&&a[2][2]=='/')
		        ||  (a[0][2]=='/'&&a[1][1]=='/'&&a[2][0]=='/')) {
			return 0;
		}
		for(int i=0; i<3; i++) {
			for(int j=0; j<3; j++) {
				if(a[i][j]=='*') {
					C(4);
				} else if(a[i][j]=='/') {
					C(9);
				}
				cout<<a[i][j]<<" ";
				C(15);
			}
			cout<<"\n";
		}
		int cnt=0;
		for(int i=0; i<3; i++) {
			for(int j=0; j<3; j++) {
				if(a[i][j]=='*'||a[i][j]=='/') {
					cnt++;
				}
			}
		}
		if(cnt==9) {
			f=1;
		}
		if(f==1) {
			break;
		}
		srand(time(NULL));
		int i=1+rand()%3,j=1+rand()%3;
		while(a[i-1][j-1]=='*' || a[i-1][j-1]=='/') {
			i=1+rand()%3,j=1+rand()%3;
		}
		i--,j--;
		a[i][j]='/';
		Sleep(3000);
		system("cls");
		// 0 1 2
		//0* * *
		//1* * *
		//2* * *
		if(a[0][0]=='*'&&a[0][1]=='*'&&a[0][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		if(a[1][0]=='*'&&a[1][1]=='*'&&a[1][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		if(a[2][0]=='*'&&a[2][1]=='*'&&a[2][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		//142~144 行
		if(a[0][0]=='*'&&a[1][0]=='*'&&a[2][0]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		if(a[0][1]=='*'&&a[1][1]=='*'&&a[2][1]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		if(a[0][2]=='*'&&a[1][2]=='*'&&a[2][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		//147~149 列
		if(a[0][0]=='*'&&a[1][1]=='*'&&a[2][2]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		if(a[0][2]=='*'&&a[1][1]=='*'&&a[2][0]=='*') {
			system("cls");
			C(4);
			cout<<"Win\n";
			C(0);
		}
		//150~151 斜
		//142~152 Win
		if(a[0][0]=='/'&&a[0][1]=='/'&&a[0][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[1][0]=='/'&&a[1][1]=='/'&&a[1][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[2][0]=='/'&&a[2][1]=='/'&&a[2][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		//154~156 行
		if(a[0][0]=='/'&&a[1][0]=='/'&&a[2][0]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[0][1]=='/'&&a[1][1]=='/'&&a[2][1]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[0][2]=='/'&&a[1][2]=='/'&&a[2][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		//158~160 列
		if(a[0][0]=='/'&&a[1][1]=='/'&&a[2][2]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		if(a[0][2]=='/'&&a[1][1]=='/'&&a[2][0]=='/') {
			system("cls");
			cout<<"Lose\n";
		}
		//162~163 斜
		//154~164 Lose
		if( (a[0][0]=='*'&&a[0][1]=='*'&&a[0][2]=='*')
		        ||  (a[1][0]=='*'&&a[1][1]=='*'&&a[1][2]=='*')
		        ||  (a[2][0]=='*'&&a[2][1]=='*'&&a[2][2]=='*')
		        ||  (a[0][0]=='*'&&a[1][0]=='*'&&a[2][0]=='*')
		        ||  (a[0][1]=='*'&&a[1][1]=='*'&&a[2][1]=='*')
		        ||  (a[0][2]=='*'&&a[1][2]=='*'&&a[2][2]=='*')
		        ||  (a[0][0]=='*'&&a[1][1]=='*'&&a[2][2]=='*')
		        ||  (a[0][2]=='*'&&a[1][1]=='*'&&a[2][0]=='*')
		        ||  (a[0][0]=='/'&&a[0][1]=='/'&&a[0][2]=='/')
		        ||  (a[1][0]=='/'&&a[1][1]=='/'&&a[1][2]=='/')
		        ||  (a[2][0]=='/'&&a[2][1]=='/'&&a[2][2]=='/')
		        ||  (a[0][0]=='/'&&a[1][0]=='/'&&a[2][0]=='/')
		        ||  (a[0][1]=='/'&&a[1][1]=='/'&&a[2][1]=='/')
		        ||  (a[0][2]=='/'&&a[1][2]=='/'&&a[2][2]=='/')
		        ||  (a[0][0]=='/'&&a[1][1]=='/'&&a[2][2]=='/')
		        ||  (a[0][2]=='/'&&a[1][1]=='/'&&a[2][0]=='/')) {
			return 0;
		}
	}
	cout<<"Tie\n";
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++井字棋游戏代码可以使用EasyX图形库进行实现,下面是一个简单的示例代码: ``` #include <graphics.h> #include <conio.h> const int kBoardSize = 3; // 棋盘大小 // 绘制棋盘 void DrawBoard() { cleardevice(); // 清空屏幕 int width = getwidth() / kBoardSize; int height = getheight() / kBoardSize; // 绘制横线 for (int i = 1; i < kBoardSize; i++) { line(0, i * height, getwidth(), i * height); } // 绘制竖线 for (int i = 1; i < kBoardSize; i++) { line(i * width, 0, i * width, getheight()); } } // 下棋 void PlayChess(int row, int col, bool is_black) { int x = col * getwidth() / kBoardSize + getwidth() / kBoardSize / 2; int y = row * getheight() / kBoardSize + getheight() / kBoardSize / 2; if (is_black) { setcolor(BLACK); circle(x, y, getwidth() / kBoardSize / 2 - 10); } else { setcolor(WHITE); line(x - getwidth() / kBoardSize / 2 + 10, y - getheight() / kBoardSize / 2 + 10, x + getwidth() / kBoardSize / 2 - 10, y + getheight() / kBoardSize / 2 - 10); line(x - getwidth() / kBoardSize / 2 + 10, y + getheight() / kBoardSize / 2 - 10, x + getwidth() / kBoardSize / 2 - 10, y - getheight() / kBoardSize / 2 + 10); } } int main() { initgraph(600, 600); DrawBoard(); bool is_black = true; // 黑棋先手 while (true) { if (_kbhit()) { // 如果有按键按下 char ch = _getch(); if (ch == 27) break; // 如果是Esc键,退出游戏 int row = -1; int col = -1; bool can_put = false; switch (ch) { case 'q': row = 0; col = 0; can_put = true; break; case 'w': row = 0; col = 1; can_put = true; break; case 'e': row = 0; col = 2; can_put = true; break; case 'a': row = 1; col = 0; can_put = true; break; case 's': row = 1; col = 1; can_put = true; break; case 'd': row = 1; col = 2; can_put = true; break; case 'z': row = 2; col = 0; can_put = true; break; case 'x': row = 2; col = 1; can_put = true; break; case 'c': row = 2; col = 2; can_put = true; break; } if (can_put) { PlayChess(row, col, is_black); is_black = !is_black; } } } closegraph(); return 0; } ``` 此示例中,通过`initgraph`函数初始化图形界面,然后在`DrawBoard`函数中绘制棋盘,`PlayChess`函数用于在棋盘上下棋。在`main`函数中,通过`_kbhit`函数检测键盘是否有按键按下,然后根据按键的不同来确定落子的位置,通过`PlayChess`函数在棋盘上下棋,并通过`is_black`变量来控制黑白棋的交替。当按下Esc键时,游戏退出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值