C++ hello there, i need your help with this C++ program. i am not a computer sci
ID: 3764266 • Letter: C
Question
C++
hello there,
i need your help with this C++ program. i am not a computer science major and programming is hard for me.
i am doing a conncet 4 game and i do not know why my code is not working!
can you help me please?
here is the code:
here is the program:
1 #include«iostream» 2 #include-cstring> 4 using namespace std; 6 int Checking) 7 int color); 8 int range); 9 void board); 11 const int ROW-6; 12 const int COL-7; 13 int array [ROh] [COL]={0,0); 14 char Red'r 15 char Yeley'; 16 char player1-Red; 17 char player2-Yel; The main function oolololelolololololk/ 19 int main) 20 int win; 21 win- Checking); 22 board); 23 color); 24 range); 25 board); 26 if (win 1){ 27 coutExplanation / Answer
Answer :
#include <iostream.h>
#include <windows.h>
#include <stdlib.h>
#define HEIGHT 6
#define WIDTH 7
void player_move(int player);
bool check_winner(int x, int y, int player);
bool check_diagonal_SW_NE(int x, int y, int player);
bool check_diagonal_NW_SE(int x, int y, int player);
bool check_vertical(int x, int y, int player);
bool check_horizontal(int x, int y, int player);
void display_board();
int board_info[HEIGHT][WIDTH] = {{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}};
int LastMoveX, LastMoveY;
int main()
{
int counter = 0;
bool winner = false;
srand(GetTickCount());
cout << "Please select a number from 1-7" << endl;
cout << "| 1| 2| 3| 4| 5| 6| 7" << endl;
cout << "---------------------";
display_board();
for (int i = 0; i < 21; i++)
{
player_move(1);
display_board();
winner = check_winner(LastMoveX, LastMoveY, 1);
if (winner)
{
cout << " You Win" << endl;
break;
}
player_move(2);
display_board();
winner = check_winner(LastMoveX, LastMoveY, 2);
if (winner)
{
cout << " You Win" << endl;
break;
}
}
system("PAUSE");
return 0;
}
void display_board()
{
cout << endl;
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
cout << "| ";
if (board_info[y][x] == 0) cout << " ";
else if (board_info[y][x] == 1) cout << "X";
else if (board_info[y][x] == 2) cout << "O";
}
cout << " ---------------------" << endl;
}
}
void player_move(int player)
{
int choice;
cout << " Player" << player << ", please select a number from 1 - 7: ";
cin >> choice;
if (cin.fail())
{
cout << "Error!";
exit(1);
}
while (choice > WIDTH || choice <=0)
{
cout << " Please select again: ";
cin >> choice;
}
int number = 0;
while (board_info[(HEIGHT-1)-number][(choice-1)] != 0)
{
number++;
if (number > (HEIGHT-1))
{
cout << " Please select again: ";
cin >> choice;
number = 0;
}
};
board_info[(HEIGHT-1)-number][choice-1] = player;
LastMoveY = (HEIGHT-1)-number;
LastMoveX = choice-1;
}
bool check_winner(int x, int y, int player)
{
bool winner;
if (check_diagonal_SW_NE(x,y,player)) return true;
else if (check_diagonal_NW_SE(x,y,player)) return true;
else if (check_vertical(x,y,player)) return true;
else if (check_horizontal(x,y,player)) return true;
else return false;
}
bool check_diagonal_SW_NE(int x, int y, int player)
{
int score = 1;
int count = 1;
while((y-count >= 0) && (x+count < WIDTH))
{
if (board_info[y-count][x+count] == player)
{
score++;
count++;
}
else break;
}
count = 1;
while((y+count < HEIGHT) && (x-count >=0))
{
if (board_info[y+count][x-count] == player)
{
score++;
count++;
}
else break;
}
if (score == 4) return true;
else return false;
}
bool check_diagonal_NW_SE(int x, int y, int player)
{
int score = 1;
int count = 1;
while((y+count >= 0) && (x+count < WIDTH))
{
if (board_info[y+count][x+count] == player)
{
score++;
count++;
}
else break;
}
count = 1;
while((y-count < HEIGHT) && (x-count >=0))
{
if (board_info[y-count][x-count] == player)
{
score++;
count++;
}
else break;
}
if (score == 4) return true;
else return false;
}
bool check_vertical(int x, int y, int player)
{
int score = 1;
int count = 1;
while(y+count >= 0 && y+count < HEIGHT)
{
if (board_info[y+count][x] == player)
{
score++;
count++;
}
else break;
}
if (score == 4) return true;
else return false;
}
bool check_horizontal(int x, int y, int player)
{
int score = 1;
int count = 1;
while((x+count >= 0) && (x+count < WIDTH))
{
if (board_info[y][x+count] == player)
{
score++;
count++;
}
else break;
}
count = 1;
while((x-count < WIDTH) && (x-count >=0))
{
if (board_info[y][x-count] == player)
{
score++;
count++;
}
else break;
}
if (score == 4) return true;
else return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.