三子棋小游戏的第二版,后期续更!!!
#include<iostream>
#include<cstdio>
#include<windows.h>
#include<ctime>
using namespace std;
int checkerboard[4][4];
/*============== all class ==============*/
class chess {
public:
static void Game_game_pvp();
static void Game_game_pvc();
};
/*============== all function ==============*/
int Game_init(){
printf(
" Three character chess game\n"
"\n"
" 1.enter 2.exit\n"
);
int retreat;
scanf("%d",&retreat);
return retreat;
}
void slow_o(string a,bool wrap){
for(int i=0;i<a.size();i++){
cout<<a[i];
Sleep(50);
}
if(wrap){
printf("\n");
}
}
void Game_user(){
printf(
" user interface\n"
" \n"
" 1.Start the game!\n"
" \n"
" 2.version\n"
" \n"
" 3.What the author said"
);
}
void Game_version(){
slow_o("Version: 0.0.2",1);
slow_o("Author:--",0);
}
void Game_author_said(){
slow_o(" This is a three piece chess game that currently only has",1);
slow_o("",1);
slow_o("people vs people and people vs computer, and more versions",1);
slow_o("",1);
slow_o("will be published in the future.",0);
}
char convert(int a){
if(a==0) return '?';
if(a==1) return 'O';
if(a==2) return 'X';
}
void round(){
int i,j;
do{
i=rand()%3+1;
j=rand()%3+1;
}while(checkerboard[i][j]);
cout<<i<<' '<<j;
checkerboard[i][j]=2;
}
void Game_interface(){
system("cls");
system("color 1");
Game_user();
int x;
while(x!=1){
slow_o("",1);
scanf("%d",&x);
if(x==1){
system("cls");
return;
}
if(x==2){
system("cls");
Game_version();
}else if(x==3){
system("cls");
Game_author_said();
}
Sleep(5000);
system("cls");
Game_user();
}
}
void opt(){
system("color 5");
printf(
"1.People vs. People(Normal mode)\n"
"\n"
"2.People vs. Computers(Expert mode)"
);
}
int determine(){
bool flag=0;
if(checkerboard[1][1]==checkerboard[1][2]&&checkerboard[1][2]==checkerboard[1][3]){
if(checkerboard[1][1]==1) return 1;
if(checkerboard[1][1]==2) return 2;
}
if(checkerboard[2][1]==checkerboard[2][2]&&checkerboard[2][2]==checkerboard[2][3]){
if(checkerboard[2][1]==1) return 1;
if(checkerboard[2][1]==2) return 2;
}
if(checkerboard[3][1]==checkerboard[3][2]&&checkerboard[3][2]==checkerboard[3][3]){
if(checkerboard[3][1]==1) return 1;
if(checkerboard[3][1]==2) return 2;
}
if(checkerboard[1][1]==checkerboard[2][1]&&checkerboard[2][1]==checkerboard[3][1]){
if(checkerboard[1][1]==1) return 1;
if(checkerboard[1][1]==2) return 2;
}
if(checkerboard[1][2]==checkerboard[2][2]&&checkerboard[2][2]==checkerboard[3][2]){
if(checkerboard[1][2]==1) return 1;
if(checkerboard[1][2]==2) return 2;
}
if(checkerboard[1][3]==checkerboard[2][3]&&checkerboard[2][3]==checkerboard[3][3]){
if(checkerboard[1][3]==1) return 1;
if(checkerboard[1][3]==2) return 2;
}
if(checkerboard[1][1]==checkerboard[2][2]&&checkerboard[2][2]==checkerboard[3][3]){
if(checkerboard[1][1]==1) return 1;
if(checkerboard[1][1]==2) return 2;
}
if(checkerboard[1][3]==checkerboard[2][2]&&checkerboard[2][2]==checkerboard[3][1]){
if(checkerboard[1][3]==1) return 1;
if(checkerboard[1][3]==2) return 2;
}
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(convert(checkerboard[i][j])=='?'){
flag=1;
}
}
}
if(flag==0){
return 0;
}
}
void stamp(){
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
cout<<convert(checkerboard[i][j])<<' ';
}
cout<<endl;
}
}
void cls(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
checkerboard[i][j]=0;
}
}
}
void chess::Game_game_pvp(){
system("cls");
cls();
system("color F");
int p1_x,p1_y;
int p2_x,p2_y;
for(int i=1;;i++){
if(determine()==0){
system("cls");
MessageBox(GetForegroundWindow()," draw","The outcome has been divided",MB_OK);
abort();
}
if(determine()==1){
system("cls");
MessageBox(GetForegroundWindow()," Player2 lost, you won","The outcome has been divided",MB_OK);
abort();
}
if(determine()==2){
system("cls");
MessageBox(GetForegroundWindow()," Player1 lost, you won","The outcome has been divided",MB_OK);
abort();
}
if(determine()==0){
system("cls");
MessageBox(GetForegroundWindow()," draw","The outcome has been divided",MB_OK);
abort();
}
stamp();
if(i%2==1){
cout<<"Please input rows and columns for player 1 : ";
cin>>p1_x>>p1_y;
checkerboard[p1_x][p1_y]=1;
system("cls");
if(determine()==0){
system("cls");
MessageBox(GetForegroundWindow()," draw","The outcome has been divided",MB_OK);
abort();
}
}else{
cout<<"Please input rows and columns for player 2 : ";
cin>>p1_x>>p1_y;
checkerboard[p1_x][p1_y]=2;
system("cls");
if(determine()==0){
system("cls");
MessageBox(GetForegroundWindow()," draw","The outcome has been divided",MB_OK);
abort();
}
}
}
}
void Game_mode(){
chess np;
int xz;
opt();
cin>>xz;
if(xz==1){
np.Game_game_pvp();
}else if(xz==2){
np.Game_game_pvc();
}
}
void chess::Game_game_pvc(){
system("cls");
cls();
system("color F");
int p_x,p_y;
int c_x,c_y;
for(int i=1;;i++){
c_x=rand()%3+1;
c_y=rand()%3+1;
if(determine()==1){
system("cls");
MessageBox(GetForegroundWindow()," computer lost, you won","The outcome has been divided",MB_OK);
abort();
}
if(determine()==2){
system("cls");
MessageBox(GetForegroundWindow()," you lost, computer won","The outcome has been divided",MB_OK);
abort();
}
if(i%2==1){
stamp();
cout<<"Please input rows and columns for player : ";
cin>>p_x>>p_y;
checkerboard[p_x][p_y]=1;
system("cls");
}else{
stamp();
cout<<"Please input rows and columns for computer : ";
Sleep(1000);
round();
Sleep(950);
system("cls");
}
}
}
/*============== The game is about to begin ! ==============*/
int main(){
srand(time(nullptr));
system("color 7");
if(Game_init()==2){
system("color 4"); return 0;
}else Game_interface();
chess myclass;
Game_mode();
return 0;
}