-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
128 lines (120 loc) · 3.06 KB
/
main.cpp
File metadata and controls
128 lines (120 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <unistd.h>
using namespace std;
void randomchess(int hang,int lie,bool chess[26][100])//随机生成棋盘
{
srand(time(0));
for(int i=0;i<hang;i++)
for(int j=0;j<lie;j++)
chess[i][j]=rand()%2;
}
void reverse_hang(int hang,int lie,bool chess[26][100],char tgt_hang)//翻一行
{
for(int i=0;i<lie;i++)
if(chess[tgt_hang-'a'][i]==1) chess[tgt_hang-'a'][i]=0;
else chess[tgt_hang-'a'][i]=1;
}
void reverse_lie(int hang,int lie,bool chess[26][100],int tgt_lie)//翻一列
{
for(int i=0;i<hang;i++)
if(chess[i][tgt_lie-1]==1) chess[i][tgt_lie-1]=0;
else chess[i][tgt_lie-1]=1;
}
void showchess(int hang,int lie,bool chess[26][100])//显示当前棋盘
{
system("cls");
cout<<"当前棋盘状态为"<<endl;
cout<<" ";
for(int i=1;i<=lie;i++)
{
cout<<i;
if(i>=10) cout<<" ";
else cout<<" ";
}
cout<<endl;
for(int i=0;i<hang;i++)
{
cout<<char(i+'a')<<" ";
for(int j=0;j<lie;j++)
if (chess[i][j]==1) cout<<'X'<<" ";
else cout<<" ";
cout<<endl;
}
}
int main()
{
SetConsoleOutputCP(936);
cout<<endl<<endl<<endl;
cout<<" Chessgame Simulation"<<endl;
cout<<"--an auxiliary tool for <Linear Algebra (Honor)>"<<endl;
cout<<" Programed by kentliukx"<<endl;
cout<<endl<<endl<<endl;
sleep(3);
system("cls");
int hang,lie,temp,tgt_lie;
char tgt_hang;
bool chess[26][100]={0};
while(1)
{
cin.sync();
cout<<"请输入行数(1-26)"<<endl;
cin>>hang;
if(hang<=26&&hang>=1) break;
cout<<"无效输入"<<endl;
}
while(1)
{
cin.sync();
cout<<"请输入列数(1-99)"<<endl;
cin>>lie;
if(lie<=99&&lie>=1) break;
cout<<"无效输入"<<endl;
}
cout<<"请输入初始棋盘状态(0或1,空格隔开),无效输入则为随机生成"<<endl;
int i=0;
for(i=0;i<hang;i++)
{
int j=0;
for(j=0;j<lie;j++)
{
cin>>temp;
if(temp==0) chess[i][j]=0;
else if(temp==1) chess[i][j]=1;
else break;
}
if (j!=lie) break;
}
if (i!=hang) randomchess(hang,lie,chess);
showchess(hang,lie,chess);
while(1)
{
cin.sync();
cout<<"输入需要翻的行或者列的序号"<<endl;
if(cin>>tgt_lie)
{
if(tgt_lie>=1 && tgt_lie<=lie)
reverse_lie(hang,lie,chess,tgt_lie);
else
{
cout<<"无效输入"<<endl;
continue;
}
}
else
{
cin.clear();
cin>>tgt_hang;
if(tgt_hang>='a'&&tgt_hang<='a'+hang-1)
reverse_hang(hang,lie,chess,tgt_hang);
else
{
cout<<"无效输入"<<endl;
continue;
}
}
showchess(hang,lie,chess);
}
}