井字棋,英文名叫Tic-Tac-Toe,是一種在3*3格子上進行的連珠游戲,和五子棋類似,由于棋盤一般不畫邊框,格線排成井字故得名,游戲需要的工具僅為紙和筆,然后由分別代表O和X的兩個游戲者輪流在格子里留下標記(一般來說先手者為X),任意三個標記形成一條直線,則為獲勝,
撰寫程式,實作簡單的井字棋游戲,基本功能包括:先手選擇(計算機或玩家先走)、列印棋盤、輸入坐標來落子、判斷輸贏或平局、有棋子的位置不能落子等,
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<stdlib.h>
using namespace std;
void insepct(int a[3][3])
{
if(a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[1][0]==a[1][1]&&a[1][0]==a[1][2]&&a[1][0]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[2][0]==a[2][1]&&a[2][0]==a[2][2]&&a[2][0]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[0][0]==a[1][0]&&a[0][0]==a[2][0]&&a[0][0]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[0][1]==a[1][1]&&a[0][1]==a[2][1]&&a[0][1]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[0][2]==a[1][2]&&a[0][2]==a[2][2]&&a[0][2]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[0][0]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[0][2]==a[1][1]&&a[0][2]==a[2][0]&&a[0][2]==1)
{
cout<<"YOU WIN"<<endl;
exit(0);
}
if(a[0][0]==a[0][1]&&a[0][0]==a[0][2]&&a[0][0]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
if(a[1][0]==a[1][1]&&a[1][0]==a[1][2]&&a[1][0]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
if(a[2][0]==a[2][1]&&a[2][0]==a[2][2]&&a[2][0]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
if(a[0][0]==a[1][0]&&a[0][0]==a[2][0]&&a[0][0]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
if(a[0][1]==a[1][1]&&a[0][1]==a[2][1]&&a[0][1]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
if(a[0][2]==a[1][2]&&a[0][2]==a[2][2]&&a[0][2]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
if(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[0][0]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
if(a[0][2]==a[1][1]&&a[0][2]==a[2][0]&&a[0][2]==2)
{
cout<<"YOU FAIL"<<endl;
exit(0);
}
}
void output(int a[3][3])
{
for (int x = 0;x < 3;x++)
{
cout << "-------" << endl;
cout << "|";
for (int y = 0;y < 3;y++)
{
if (a[x][y] == 0)
{
cout << " ";
}
if (a[x][y] == 1)
{
cout << "O";
}
if (a[x][y] == 2)
{
cout << "X";
}
cout << "|";
}
cout << endl;
}
cout << "-------" << endl;
}
void chess()
{
int b, c, n, x, y, i;
int a[3][3] = {
0 }
;
cout << "請選擇先手(1 - 電腦 0 - 玩家)" << endl;
cin >> n;
cout <<"-------"<<"\n"<< "| | | |" << "\n" << "-------" << "\n" << "| | | |" << "\n" << "-------" << "\n" << "| | | |" <<"\n"<< "-------" << endl;
if (n == 0)
{
for(i=0;i<9;i++)
{
cout << "請輸入棋子坐標" << endl;
cin >> b >> c;
a[b][c] = 1;
//1為玩家O,2為電腦X
output(a);
insepct(a);
b=rand()%3;
c=rand()%3;
while(a[b][c]==1)
{
b=rand()%3;
c=rand()%3;
}
a[b][c]=2;
output(a);
insepct(a);
}
}
if (n == 1)
{
for(i=0;i<9;i++)
{
b=rand()%3;
c=rand()%3;
while(a[b][c]==1)
{
b=rand()%3;
c=rand()%3;
}
a[b][c]=2;
output(a);
insepct(a);
cout<<"請輸入棋子坐標"<<endl;
cin>>b>>c;
a[b][c]=1;
output(a);
insepct(a);
}
}
}
int main()
{
int h, b;
cout << "********************" << endl;
cout << "*****1.開始游戲*****" << endl;
cout << "*****0.結束游戲*****" << endl;
cout << "********************" << endl;
srand((unsigned)time(NULL));
cin >> h;
if (h == 0)
{
return 0;
}
if (h == 1)
{
chess();
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/389277.html
標籤:其他
上一篇:Unity中的資源管理-參考計數
