这是我的连接四游戏的代码。我有运行和编译的工作,但当我选择在第6列中放置一个四连接时,PuTTY编译器会在命令行中自动放置单词“PuTTY”,当它再次询问我想要放置另一块时的列。这只发生在数字6和我输入第一个6.我可以删除出现的“PuTTY”并再次输入6并且它可以工作,但“PuTTY”将再次出现在命令行中。老师助理和我都迷了。
1 #include <iostream>
2 #include <string>
3 #include <cstdlib>
4
5 #define ROWS 6
6 #define COLS 7
7
8 using namespace std;
9
10 void cout_pac(); // cout message "player one pick a color"
11 int players_colors(char); // this function will check what color player 1 chose
12 void cout_pc(int); // tells players what colors they are
13 void cout_column1();
14 char** underscores(char**);
15 char** gameboard(char**);
16 char** player1_game(int, int, char**);
17 void cout_column2();
18 char** player2_game(int, int, char**);
19 int check_endgame(char**);
20 int win_statement(int);
21 void cout_playagain();
22
23 void cout_pac() {
24 cout << "Player 1 which color do you want red or yellow? red (r) & yellow (y): ";
25 }
26
27 int players_colors(char roy) {
28 if (roy == 'r') {
29 return 1;
30 }
31
32 else {
33 return 2;
34 }
35 }
36
37 void cout_pc(int color) {
38 if (color == 1)
39 cout << "Player one is red and player 2 is yellow!" << endl;
40 if (color == 2)
41 cout << "Player one is yellow and player 2 is red!" << endl;
42 }
43
44 void cout_column1() {
45 cout << "Player 1 which coloumn would like to put your piece in?: ";
46 }
47
48 char** underscores(char** board) {
49 for (int i = 0; i < ROWS; i++) {
50 for (int j = 0; j < COLS; j++) {
51 board[i][j] = ' ';
52 }
53 }
54 return board;
55 }
56
57 char** gameboard(char** array) {
58 for (int i = 0; i < ROWS; i++) {
59 cout << "| ";
60 for (int j = 0; j < COLS; j++) {
61 cout << array[i][j] << " | ";
62 }
63 cout << endl;
64 cout << "-----------------------------" << endl;
65 }
66 return array;
67 }
68
69 char** player1_game(int color, int column, char** array) {
70 char j, c, r, y;
71 j = column-1;
72
73 if (color == 1) {
74 c = 'r';
75 }
76
77 if (color == 2) {
78 c = 'y';
79 }
80
81 for (int i = ROWS-1; i >= 0; i--) {
82 cout << "hey" << endl;
83 if (array[i][j] == ' ') {
84 array[i][j] = c;
85 cout << i << j << endl;
86 break;
87 }
88 }
89 return array;
90 }
91
92 void cout_column2() {
93 cout << "Player 2 which column would you like to put your piece in?: ";
94 }
95
96 char** player2_game(int color, int column, char** array) {
97 char j, c, r, y;
98 j = column-1;
99
100 if (color == 1) {
101 c = 'y';
102 }
103
104 if (color == 2) {
105 c = 'r';
106 }
107 for (int i = ROWS-1; i >= 0; i--) {
108 cout << "hey 2" << endl;
109 if (array[i][j] == ' ') {
110 array[i][j] = c;
111 cout << i << j << endl;
112 break;
113 }
114 }
115 return array;
116 }
117
118 int check_endgame(char** array) {
119 int gameover = 1;
120 for (int i = ROWS-1; i >= 0; i--) {
121 for (int j = COLS-1; j >= 0; j--) {
122 if (array[i][j] == ' ') {
123 gameover = 0;
124 }
125 }
126 }
127 return gameover;
128 }
129
130 int win_statement(int gameover) {
131 int game;
132 if (gameover == 0) {
133 game == 1;
134 }
135 if (gameover == 1) {
136 cout << "Game board has been filled, no one wins!" << endl;
137 game = 0;
138 }
139 if (gameover == 2) {
140 game = 0;
141 }
142 return game;
143 }
144
145 void cout_playagain() {
146 cout << "Would you like to play again? yes (1) no (0): " << endl;
147 }
148
149 int main() {
150 char** board = new char*[ROWS];
151 for (int i = 0; i < COLS; i++) {
152 board[i] = new char [COLS];
153 } // creates a 6x7 array
154 char roy;
155 char** array; // player's choice of red or yellow, holds array board as just "array"
156 int color, column; // the color the player chose, which column they chose
157
158
159 cout_pac(); // cout message "player one pick a color"
160
161 cin >> roy; // player's choice of red or yellow
162
163 color = players_colors(roy); // this function will check what color player 1 chose
164 cout_pc(color); // tells players what colors they are
165
166 array = underscores(board); // fills board with underscores then returns board as "array"
167 array = gameboard(array); // turns array into a visual gameboard
168 cout_column1(); // asks player 1 to pick a column
169 cin >> column; // gets which column player wants
170
171 bool game;
172 while (game, color, column, array) {
173 int gameover;
174 array = player1_game(color, column, array); // takes in column and returns new array for player 1
175
176 gameboard(array); // prints game board after player 1 has added their piece
177 gameover = check_endgame(array); // checks if game board was filled without winning
178 game = win_statement(gameover); // checks all end game statuses and tells user how game was ended
179
180 cout_column2(); // asks player 2 for a column to put a piece in
181 cin >> column; // gets column number from player 2
182
183 array = player2_game(color, column, array); // takes in column from player 2 and returns new array for player 2
184
185 gameboard(array); // prints the game board after player 2 added their piece
186 check_endgame(array); // checks if gameboard was filled without winning
187 game = win_statement(gameover); //checks all end game statuses and tells user how game was ended
188
189 cout_column1();
190 cin >> column;
191
192 }
193 //cout_playagain(); // cout asks if user wants to play again yes or no
194 //cin >> again;
195
196
197
198
199 return 0;
200 }
201
这就是它的样子:
答案 0 :(得分:1)
当您输出ASCII值为PuTTY
的控制字符时,会显示5
消息,如here所述。
您使用player1_game
调用的column == 6
函数输出此字符:
char** player1_game(int color, int column, char** array) {
char j, c, r, y;
j = column-1;
现在j
是5
,然后你会这样做:
cout << i << j << endl;
由于j
是char
,因此输出带有ascii代码5
的字符。 5
与'5'
不同。
要解决此问题,您应将j
声明为int
而不是char
。 player2_game
有同样的问题。当您尝试同时输出i
和i
时,您的输出屏幕仅显示j
,这似乎不奇怪吗?
您的代码包含许多其他奇怪的事情和错误。如果可能,尝试在编译器上启用所有警告。一些问题是:
game == 1;
应为game = 1;
while (game, color, column, array)
是无稽之谈;它应该是while(1)
并且当玩家赢得游戏时你需要在你的循环中放置一个break
语句。目前,如果玩家1赢了,你就直接去请求玩家2的移动check_endgame(array);
while
循环内)。column - 1
用作数组索引之前检查cout_column1();
是否超出范围cin >> column;
和char **
出现在循环之前和循环结束时;而不是这个,把它们放在循环的开头。避免代码重复总是好的,并且复制粘贴(正如你明确对player1_game和player2_game所做的那样)表明事情并不完全正确。许多函数不必要地返回char** array;
,并且您的代码使用的返回值没有多大意义。完全摆脱board
并使用underscores(board);
gameboard(board);
,例如:
char **
将所有返回void
的函数更改为实际返回sed