猫史档案馆


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

用户:程序员李某_com程序员李某_com查看:0 回复:5 评论:0 创建时间:2021-07-27T10:09:41


先打个广告,代码在后面

上代码:

#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++){
		    	S
				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;
}


回复

上一页1 页 / 共 1下一页
程序员黄某程序员黄某

多了一个S

 

点赞0


评论


程序员黄某程序员黄某

#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;
}

点赞0


评论


程序员李某_com程序员李某_com

@老MC 对

 

点赞0


评论


MC东方墨染殇雪就是秀MC东方墨染殇雪就是秀

nbnb!

点赞0


评论


KIHIOKIHIO

你试试我这个PYTHON的?

def jd(x):
    try:
        x = float(x)
    except ValueError:
        return 0
    if x-(float(int(x))) > 0:
        return 0
    x = int(x)
    if x < 1 or x > 10:
        return 0
    return x


def sy(a, b, c, a2, b2, c2, s):
    x = 0
    while x <= a2:
        y = 0
        while y <= b2:
            n = 0
            lxh = 0
            lxb = 0
            while n <= c2:
                if s[x+y+n] == '黑':
                    lxh += 1
                else:
                    lxh = 0
                if s[x+y+n] == '白':
                    lxb += 1
                else:
                    lxb = 0
                n += c
            if lxb == 5:
                return 0
            if lxh == 5:
                return 2
            y += b
        x += a
    return 1


def xsy(xx, xs, yx, ys, js, js2, s):
    x = xx
    while x <= xs:
        y = yx
        while y <= yx:
            lxb = 0
            lxh = 0
            n = 0
            while n <= 4:
                if s[(y+n*js2)*10+x+n*js] == '黑':
                    lxh += 1
                else:
                    lxh = 0
                if s[(y+n*js2)*10+x+n*js] == '白':
                    lxb += 1
                else:
                    lxb = 0
                n += 1
            if lxb == 5:
                return 0
            if lxh == 5:
                return 2
            y += 1
        x += 1
    return 1


print("棋盘为10*10。")
s = []
a = 0
while a < 100:
    s.append('无')
    a += 1
print("你告诉我黑棋还是白棋先下?(输入A代表黑,输入B代表白):")
while True:
    hb = input()
    if hb != 'A' and hb != 'B':
        print("输入错误,请重试:")
    else:
        break
b = 0
while True:
    f = 0
    fh = 0
    hqs = 0
    bqs = 0
    if sy(10, 1, 1, 90, 5, 4, s) == 2:
        hqs = 1
    if sy(1, 10, 10, 9, 50, 40, s) == 2:
        hqs = 1
    if sy(10, 1, 1, 90, 5, 4, s) == 0:
        bqs = 1
    if sy(1, 10, 10, 9, 50, 40, s) == 0:
        bqs = 1
    if xsy(0, 5, 0, 5, 1, 1, s) == 2:
        hqs = 1
    if xsy(4, 9, 4, 9, -1, -1, s) == 2:
        hqs = 1
    if xsy(0, 5, 0, 5, 1, 1, s) == 0:
        bqs = 1
    if xsy(4, 9, 4, 9, -1, -1, s) == 0:
        bqs = 1
    if xsy(0, 5, 4, 9, 1, -1, s) == 2:
        hqs = 1
    if xsy(0, 5, 4, 9, 1, -1, s) == 0:
        bqs = 1
    if xsy(4, 9, 0, 5, -1, 1, s) == 2:
        hqs = 1
    if xsy(4, 9, 0, 5, -1, 1, s) == 0:
        bqs = 1
    while f < 100:
        k = 0
        bs = ' '
        while k < 10:
            if k == 0:
                bs = s[f+k]
            else:
                bs = bs+' '+s[f+k]
            k += 1
        print(bs)
        f += 10
    if hqs == 1:
        print("黑棋赢!")
        break
    if bqs == 1:
        print("白棋赢!")
        break
    if hb == 'A':
        if b % 2 == 0:
            jhb = 'A'
        else:
            jhb = 'B'
    else:
        if b % 2 == 0:
            jhb = 'B'
        else:
            jhb = 'A'
    if jhb == 'A':
        print("黑棋的x坐标(从左到右,1~10):")
        while True:
            x = input()
            if jd(x) == 0:
                print("输入错误,请重试:")
            else:
                x = jd(x)
                break
        print("黑棋的y坐标(从上到下,1~10):")
        while True:
            y = input()
            if jd(y) == 0:
                print("输入错误,请重试:")
            else:
                y = jd(y)
                break
        if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
            print("禁止重叠,下次做决策看棋盘现状。")
            fh = 1
        else:
            s[(y-1)*10+x-1] = '黑'
    else:
        print("白棋的x坐标(从左到右,1~10):")
        while True:
            x = input()
            if jd(x) == 0:
                print("输入错误,请重试:")
            else:
                x = jd(x)
                break
        print("白棋的y坐标(从上到下,1~10):")
        while True:
            y = input()
            if jd(y) == 0:
                print("输入错误,请重试:")
            else:
                y = jd(y)
                break
        if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
            print("禁止重叠,下次做决策看棋盘现状。")
            fh = 1
        else:
            s[(y-1)*10+x-1] = '白'
    if fh == 0:
        b += 1

点赞0


评论