猫史档案馆


程序员黄某

程序员黄某

Lv.1

获赞:2收藏:0浏览:14作品收藏:0
回复帖子评论
上一页1 页 / 共 1下一页

福利!c++五子棋代码!(原创) 中回复

多了一个S

 

2021-07-27T10:17:41 点赞:0

福利!c++五子棋代码!(原创) 中回复

#include <bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;

int mp[10][10];
int record[10][10];
int back[10][10];
int r=0;
int wn = 0;
int f=1;
int who=0;

void remove(int n){
	for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			if(mp[i][j]==n){
				mp[i][j]=0;
			}
		}
	}
}

void win(int x){
    for(int i=0;i<=9;i++){
		for(int j=0;j<=9;j++){
			if(mp[i][j]==0){
				break;
			}
			if(i==9&&j==9){
				cout<<endl<<"Draw";
				//exit(0);
				who = 3;
			}
		}
	}
	for(int i=0;i<=9;i++){
		for(int j=0;j<=9;j++){
			int x1=i;
			int y1=j;
		    for(int win=0;win<=4;win++){
		    	x1++;
		    	if(mp[x1][y1]!=x){
		    		break;
				}
				if(win==4){
		    		wn = 100;
		    		cout<<"player "<<x<<" win!";
		    		who = x;
		    		//exit(0);
				}
			}
	    }
	}
	for(int i=0;i<=9;i++){
		for(int j=0;j<=9;j++){
			int x1=i;
			int y1=j;
		    for(int win=0;win<=4;win++){
		    	y1--;
		    	if(mp[x1][y1]!=x){
		    		break;
				}
				if(win==4){
		    		wn = 100;
		    		cout<<"player "<<x<<" win!";
		    		//exit(0);
				}
			}
	    }
	}
	for(int i=0;i<=5;i++){
		for(int j=0;j<=5;j++){
			int x1=i;
			int y1=j;
		//	for(int x1=i;)
		    for(int win=0;win<=4;win++,x1++,y1++){
		    	if(mp[x1][y1]!=x){
		    		break;
				}
				if(win==4){
		    		wn = 100;
		    		cout<<"player "<<x<<" win!";
		    		//exit(0);
				}
			}
	    }
	}
	for(int i=9;i<=4;i--){
	for(int j=9;j<=4;j--){
			int x1=i;
			int y1=j;
		//	for(int x1=i;)
		    for(int win=0;win<=4;win++){
		    	x1--;
		    	y1--;
		    	if(mp[x1][y1]!=x){
		    		break;
				}
				if(win==4){
		    		wn = 100;
		    		cout<<"player "<<x<<" win!";
		    		//exit(0);
				}
			}
	    }
	}
	return;
}

void print(){
	system("cls");
	cout<<"                        Gobang                           "<<endl;
	cout<<"player "<<f<<" Go chess pieces"<<endl;
	cout<<"Play:"<<endl<<"1.This game requires 2 people"<<endl<<"2.Use w, s, a, d to control coordinates"<<endl<<"3.□ is a selection box, ○ is white, ● is black, and black goes first"<<endl;
	cout<<"4.Both sides use black and white chess "<<endl<<"pieces respectively, which are placed"<<endl<<" at the intersection of the straight "<<endl<<"line and the horizontal line of the "<<endl<<"chessboard, and those who form the "<<endl<<"five-sub connection first win."<<endl;
	//cout<<"player 1                                         player 2"<<endl;
	
	cout<<"    "<<""; 
	for(int i=1;i<22;i++){
			if(i%2==0) cout<<"═";
			else cout<<"╬";
		}
		cout<<endl;
	for(int i=0;i<10;i++){
		if(i>=10) cout<<i;
		else cout<<0<<i;
		cout<<" ";
		cout<<"║";
		for(int j=0;j<10;j++){
			if(mp[i][j]==0){
				cout<<" ";
			}else if(mp[i][j]==2){
				cout<<"●";
			}else if(mp[i][j]==1){
				cout<<"○";
			}else if(mp[i][j]==3){
				cout<<"□";
			}
			cout<<"║";
		}
		cout<<endl<<"    ";
		for(int i=1;i<22;i++){
			if(i%2==0) cout<<"═";
			else cout<<"╬";
		}
		cout<<endl;
	}
	cout<<"      "<<"";
	for(int i=0;i<10;i++){
		if(i>=10) cout<<i<<" ";
		else cout<<0<<i<<" ";
	}
	cout<<endl;
	/*for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
		    cout<<record[i][j]<<" ";
	    }
	    cout<<endl;
	}*/
}

void play1(){
	//r++;
	f=1;
	int x=5,y=5;
	char n;
	for(;;){
	remove(3);
	n = _getch();
	Sleep(100);
	if(n=='w'){
		y--;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n=='s'){
		y++;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n=='a'){
		x--;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n=='d'){
		x++;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n==' '){
		break;
	}
}
    if(mp[y][x]==0){
    	record[y][x] = r;
	    mp[y][x] = 1;	
	}else{
		exit(0);
	}
    //mp[y][x] = 1;
	//cout<<endl;
	//cin>>x>>y;
	/*if(mp[x][y]==0){
	    mp[x][y] = 1;	
	}*/
	win(1);
	print();
	return;
}

void play2(){
	//r++;
	f=2;
	int x=5,y=5;
	char n;
	for(;;){
	remove(3);
	n = _getch();
	Sleep(100);
	if(n=='w'){
		y--;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n=='s'){
		y++;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n=='a'){
		x--;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n=='d'){
		x++;
		if(mp[y][x]==0){
	    mp[y][x] = 3;	
	    }
		print();
	}else if(n==' '){
		break;
	}
}
    if(mp[y][x]==0){
    	record[y][x] = r;
	    mp[y][x] = 2;	
	}else{
		exit(0);
	}
	//cout<<endl;
	//cin>>x>>y;
	/*if(mp[x][y]==0){
	    mp[x][y] = 2;	
	}*/
	win(2);
	print();
}

void playback(){
	int n=1;
	system("cls");
	for(int i=0;i<=r;i++){
		for(int i=0;i<10;i++){
		    for(int j=0;j<10;j++){
				if(record[i][j]==n){
				    n++;
					back[i][j] = mp[i][j];
					break;
				}
		    }	
		}
		system("cls");
		cout<<"    "<<""; 
	for(int i=1;i<22;i++){
			if(i%2==0) cout<<"═";
			else cout<<"╬";
		}
		cout<<endl;
	for(int i=0;i<10;i++){
		if(i>=10) cout<<i;
		else cout<<0<<i;
		cout<<" ";
		cout<<"║";
		for(int j=0;j<10;j++){
			if(back[i][j]==0){
				cout<<" ";
			}else if(back[i][j]==2){
				cout<<"●";
			}else if(back[i][j]==1){
				cout<<"○";
			}
			cout<<"║";
		}
		cout<<endl<<"    ";
		for(int i=1;i<22;i++){
			if(i%2==0) cout<<"═";
			else cout<<"╬";
		}
		cout<<endl;
	}
	cout<<"      "<<"";
	for(int i=0;i<10;i++){
		if(i>=10) cout<<i<<" ";
		else cout<<0<<i<<" ";
	}
	cout<<endl;
	Sleep(2000);
	}
	return;
}

int main(){
	print();
	for(;wn!=100;){
		r++;
	    play1();
	    //cout<<endl<<wn;
	    r++;
	    play2();
	    //win(2);
	    //cout<<endl<<wn;
	}
	/*cout<<"Play back? Press B for playback, and press other keys to exit"<<endl;
	char back;
	back = _getch();
	if(back=='b'){
	    playback();	
	}*/
	cout<<endl<<"Author:C++编程大佬_mc and 方块人mc"<<endl;
	cout<<endl<<"Tips:C++编程大佬_mc and 方块人mc All rights reserved,No one may reprint or plagiarize!!!"<<endl;
	exit(0);
	return 0;
}

2021-07-27T10:17:57 点赞:0