I am having trouble with my enum type in my code. I have a maze that I load and loop through with for loops, and if the char symbol is equal to '*' I want to set my 2D array enum to "Wall" and to "Clear" is the symbol is whitespace. Here is my code..
Header file containing enum initialization:
#ifndef TYPE_H
#define TYPE_H
struct coordinate
{
int row;
int col;
};
enum SquareType {Wall, Clear, Visited, Path};
#endif
Header File declaring the 2D array for the enum:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
#include "type.h"
const int MAX = 50;
#ifndef MazeClass_H
#define MazeClass_H
class MazeClass
{
private:
SquareType Maze[MAX][MAX];
coordinate Entrance, Exit;
int height, width;
public:
MazeClass();
void ReadMaze(ifstream&);
void DisplayMaze();
coordinate GetEntrance();
coordinate GetExit();
void MarkVisited(coordinate);
void MarkPath(coordinate);
bool IsWall(int x, int y);
bool IsClear(int x, int y);
bool IsPath(int x, int y);
bool IsVisited(coordinate);
bool IsExit(int x, int y);
bool IsInMaze(int x, int y);
};
#endif
My implementation file: Notice the function to Read Maze and the function to Display maze, as this is my problem area..
#include "MazeClass.h"
#include <iostream>
#include <cstdlib>
MazeClass::MazeClass()
{
}
void MazeClass::ReadMaze(ifstream& myIn)
{
int x, y;
char ch;
myIn >> x;
myIn >> y;
height = x;
width = y;
myIn >> x;
myIn >> y;
Entrance.row = x;
Entrance.col = y;
myIn >> x;
myIn >> y;
Exit.row = x;
Exit.col = y;
myIn.ignore(100, '\n');
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
myIn.get(ch);
if(ch == ' ')
Maze[i][k] == Clear;
else
Maze[i][k] == Wall;
cout << ch;
}
cout << endl;
}
}
void MazeClass::DisplayMaze()
{
char ch;
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
if(Maze[i][k] == Wall)
{
ch = '*';
cout << ch;
}
else if(Maze[i][k] == Clear)
{
ch = ' ';
cout << ch;
}
}
cout << endl;
}
}
coordinate MazeClass::GetEntrance()
{
return Entrance;
}
coordinate MazeClass::GetExit()
{
return Exit;
}
bool MazeClass::IsWall(int x, int y)
{
if(Maze[x][y] == Wall)
return true;
else
return false;
}
bool MazeClass::IsClear(int x, int y)
{
if(Maze[x][y] == Clear)
return true;
else
return false;
}
bool MazeClass::IsExit(int x, int y)
{
if(x == Exit.row && y == Exit.col)
return true;
else
return false;
}
bool MazeClass::IsInMaze(int x, int y)
{
cout << "Height: " << height << " " << "Width: " << width << endl;
if(x < 0 || x > height || y < 0 || y > width)
return false;
else
return true;
}
And finally my file to use these functions and test output:
#include "MazeClass.h"
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char *argv[])
{
MazeClass maze;
ifstream myIn;
int x,y;
string filename = argv[1]; // command line arguement stuff
myIn.open(filename.c_str());
maze.ReadMaze(myIn); //reads in the maze from a data file
maze.DisplayMaze();
cout << "The entrance is at: " << maze.GetEntrance().row << " " << maze.GetEntrance().col << endl;
cout << "The exit is at: " <<maze.GetExit().row << " " << maze.GetExit().col << endl;
if(maze.IsWall(1,1) == true) //uses the IsWall method to determine wether it is a wall or not
cout << "location (1,1) is a wall\n";
else
cout << "location (1,1) is not a wall\n";
if(maze.IsClear(1,3) == true) //uses the IsClear method to determine wether it is a clear or not
cout << "location (1,3) is clear\n";
else
cout << "location (1,3) is not clear\n";
if(maze.IsInMaze(1,5) == true) //uses the IsInMaze method to determine wether it is a clear or not
cout << "location (1,5) is in the Maze\n";
else
cout << "location (1,5) is not in the Maze\n";
if(maze.IsInMaze(25,35) == true)
cout << "location (25,35) is in the maze\n";
else
cout << "location (25,35) is not in the maze\n";
myIn.close();
return 0;
}
The problem is... I keep getting shotty output. I have included a cout on my "read Maze" function and my "display maze" function to show the difference between the two.
My output:
****************** *
* * ***** *
* ***** *** *
* ***** ***** ** *
* * * *
* ******* * *
************ *******
*********************
*********************
*********************
*********************
*********************
*********************
*********************
The entrance is at: 0 18
The exit is at: 6 12
location (1,1) is a wall
location (1,3) is not clear
Height: 7 Width: 20
location (1,5) is in the Maze
Height: 7 Width: 20
location (25,35) is not in the maze
Here is the data file i am reading from:
7 20
0 18
6 12
****************** *
* * ***** *
* ***** *** *
* ***** ***** ** *
* * * *
* ******* * *
************ *******
So as you can see, its as if the enum type always thinks that the char is a "Wall" when from the first output of the mazze its clearly not. Any suggestions? Am i missing something that I have probably looked over? Any help is appreciated. Thanks!
In ReadMaze:
Maze[i][k] == Clear;
This is a comparison. I believe you want assignment:
Maze[i][k] = Clear;
The same is true for Maze[i][k] == Wall;.
Related
I created a basic consol program that creates a box of the users desired height and width. I wanted to learn how classes worked so I just used one file. Now i'm trying to properly put the class into a .h and .cpp file. Can someone point out what I'm doing wrong and how I should fix it?
Original Code with just a main.cpp:
#include "stdafx.h"
#include <iostream>
using namespace std;
class BoxClass {
//prv variables
unsigned short int width;
int height, i;
float space_Value;
float height_Count = 1;
bool error = false;
//prv functions
void Print_Rectangle(int x, int y) {
//calc
space_Value = (3 * x) - 4;
//draw top of box
for (width = 1; width < x; width += 1) {
cout << "...";
}
cout << "\n";
//draw sides
for (height = 1; height < y; height += 1) {
cout << ":";
height_Count++;
for (width = 1; width < space_Value; width += 1) {
cout << " ";
}
cout << ":\n";
}
//draw bottom
cout << ":";
for (width = 1; width < space_Value; width += 1) {
cout << ".";
}
cout << ":\n";
}
public:
//function shows area of individual spaces
float Rectangle_Area() {
if (error == false) {
return (height_Count - .5)*(space_Value - 1);
}
else {
return 0;
}
}
//function shows area of individual spaces
// constructor
BoxClass(int x, int y, int amount) {
if (x <= 41) {
for (i = 1; i <= amount; i += 1) {
Print_Rectangle(x, y);
}
}
else {
error = true;
cout << "Error - width must be below 42!\n";
}
};
};
int main() {
//variable declaration/definition
int width_Var;
int height_Var;
int number_of_Boxes;
//object declaration/Body
cout << "Enter width of rectangle/box\nWidth = ";
cin >> width_Var;
cout << "Enter height of rectangle/box\nHeight = ";
cin >> height_Var;
cout << "How many rectangles/boxes do you want?\n";
cin >> number_of_Boxes;
BoxClass box1(width_Var, height_Var, number_of_Boxes);
cout <<"Box Area = "<<box1.Rectangle_Area() << endl;
//exit
cout << "\n\n\n\n\n";
system("pause");
return 0;
}
New Code with main.cpp, BoxClass.h, Boxclass.cpp:
main.cpp:
#include "BoxClass.h"
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
//variable declaration/definition
int width_Var;
int height_Var;
int number_of_Boxes;
//object declaration/Body
cout << "Enter width of rectangle/box\nWidth = ";
cin >> width_Var;
cout << "Enter height of rectangle/box\nHeight = ";
cin >> height_Var;
cout << "How many rectangles/boxes do you want?\n";
cin >> number_of_Boxes;
BoxClass box1(width_Var, height_Var, number_of_Boxes);
cout <<"Box Area = "<<box1.Rectangle_Area() << endl;
//exit
cout << "\n\n\n\n\n";
system("pause");
return 0;
}
BoxClass.h:
#ifndef BOXCLASS_H
#define BOXCLASS_H
class BoxClass {
//prv variables
unsigned short int width;
int height, i;
float space_Value;
float height_Count = 1;
bool error = false;
//prv functions
void Print_Rectangle(int x, int y);
public:
//function shows area of individual spaces
float Rectangle_Area();
//function shows area of individual spaces
// constructor
BoxClass(int x, int y, int amount);
};
#endif
BoxClass.cpp:
#include "BoxClass.h"
#include "stdafx.h"
#include <iostream>
using namespace std;
BoxClass::Print_Rectangle(int x, int y) {
//calc
space_Value = (3 * x) - 4;
//draw top of box
for (width = 1; width < x; width += 1) {
cout << "...";
}
cout << "\n";
//draw sides
for (height = 1; height < y; height += 1) {
cout << ":";
height_Count++;
for (width = 1; width < space_Value; width += 1) {
cout << " ";
}
cout << ":\n";
}
//draw bottom
cout << ":";
for (width = 1; width < space_Value; width += 1) {
cout << ".";
}
cout << ":\n";
}
//function shows area of individual spaces
BoxClass::Rectangle_Area() {
if (error == false) {
return (height_Count - .5)*(space_Value - 1);
}
else {
return 0;
}
}
// constructor
BoxClass::BoxClass(int x, int y, int amount) {
if (x <= 41) {
for (i = 1; i <= amount; i += 1) {
Print_Rectangle(x, y);
}
}
else {
error = true;
cout << "Error - width must be below 42!\n";
}
};
You forgot about type specifier so you should change this in your BoxClass.cpp:
BoxClass::Print_Rectangle(int x, int y)
...
BoxClass::Rectangle_Area()
to this:
void BoxClass::Print_Rectangle(int x, int y)
...
float BoxClass::Rectangle_Area()
Also a bit aside of topic, thing that I can suggest you for future is to use #pragma once instead include guards.
You need a constructor, which is where you'll set the values of height_Count and error. You should probably also define a destructor, even though at this point there is probably nothing to do there. It's just a good idea. According to Scott Meyers in Effective C++, you should also define a copy constructor and a copy assignment operator.
This is in addition to the type-specifier suggestion already posted.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to make a TicTacToe Game, but something isn't working in my code. I made a new class called TicTacToeGame and I have my source.cpp (main file). Everything was fine until I made a loop to test if a space is occupied.
Since then, it asks me to enter a coordinate for X. I give it an answer and then it asks me for a Y coordinate. I give it an answer; but, again it asks for a Y coordinate, and if I give him a bad input it start spamming me with that question or tells me that I've introduced a bad input.
What can I do? I want to know what's wrong.
TicTacToeGame.h
#pragma once
class TicTacToeGame
{
public:
TicTacToeGame();
void playGame();
private:
bool placeMarker(int x, int y, char currentPlayer);
int getXCoord();
int getYCoord();
char board[3][3];
// Clears the board
void clearBoard();
// Prints the board
void printBoard();
};
TicTacToe.cpp
#include "TicTacToeGame.h"
#include <iostream>
using namespace std;
TicTacToeGame::TicTacToeGame()
{
clearBoard();
};
void TicTacToeGame::playGame()
{
char player1 = 'X';
char player2 = 'Y';
char currentPlayer = 'X';
bool isDone = false;
int x, y;
while (isDone == false) {
printBoard();
x = getXCoord();
y = getYCoord();
if (placeMarker(x, y, currentPlayer) == false) {
cout << "That spot is occupied\n";
}
else
{
// Switch player so every player can put markers
if (currentPlayer == player1) {
currentPlayer = player2;
}
else
{
currentPlayer = player1;
}
}
}
}
int TicTacToeGame::getXCoord()
{
bool isInputBad = true;
int x;
while (isInputBad == true) {
cout << "Enter the X coordinate: ";
cin >> x;
if (x < 1 || x > 3) {
cout << "Invalid coordinate!\n";
}
else
{
isInputBad = false;
}
}
return x - 1;
}
int TicTacToeGame::getYCoord()
{
bool isInputBad = true;
int y;
while (isInputBad == true) {
cout << "Enter the Y coordinate: ";
cin >> y;
}
if (y < 1 || y > 3) {
cout << "Invalid coordinate!\n";
}
else
{
isInputBad = false;
}
return y - 1;
}
bool TicTacToeGame::placeMarker(int x, int y, char currentPlayer)
{
if (board[y][x] != ' ') {
return false;
}
board[y][x] = currentPlayer;
return true;
}
void TicTacToeGame::clearBoard()
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
}
void TicTacToeGame::printBoard()
{
cout << endl;
cout << " |1 2 3|\n";
for (int i = 0; i < 3; i++) {
cout << "--------\n";
cout << i+1 << "|" << board[i][0] << "|" << board[i][1] << "|" << board[i][2] << "|\n";
}
cout << "--------\n";
}
source.cpp
#include "TicTacToeGame.h"
#include <iostream>
using namespace std;
int main()
{
TicTacToeGame game;
game.playGame();
system("PAUSE");
return 0;
}
Your while loop will never exit,
The X Coord While Loop:
while (isInputBad == true) {
cout << "Enter the X coordinate: ";
cin >> x;
if (x < 1 || x > 3) {
cout << "Invalid coordinate!\n";
}
else
{
isInputBad = false;
}
} // end of while loop
The Y Coord While loop:
while (isInputBad == true) {
cout << "Enter the Y coordinate: ";
cin >> y;
} // end of while loop
if (y < 1 || y > 3) {
cout << "Invalid coordinate!\n";
}
else
{
isInputBad = false;
}
See how the validation code is outside of the while loop for the Y Coord?
The while condition isInputBad will always equal true because it would get set to false after the loop.
Move the end of the while loop after the if statement and it should work.
I recently received an assignment to create a bowling program in C++ that simulates two people bowling and outputs the correct score for each frame. My program works by first generating all the throws for each frame and then accumulating the score afterwards in a separate method. I was able to get the program to work when the player bowls a non-perfect game and a perfect game, but I am having problems with when a player bowls all spares. I rigged the code to make it so I have 9 for a first throw and 1 for the second throw (this is in frame.cpp). The total should be 190, but I am getting 191 and I can't seem to find the error. Each bowling class contains an array of 11 frames. I know there are only 10 frames but this is to account for if the player gets a strike on the tenth frame. Any help would be appreciated, thanks.
Here is the frame. h file
#ifndef FRAME_H
#define FRAME_H
#include<iostream>
using namespace std;
class Frame
{
private: int throw1;
int throw2;
int score;
bool spare;
bool strike;
public: Frame();
int genThrow(int size);
int getFirstThrow();
int getSecondThrow();
int getScore();
void setScore(int);
void setFirstThrow(int value1);
void setSecondThrow(int value2);
void setStrike(bool value);
void setSpare(bool value);
bool getStrike();
bool getSpare();
};
#endif
Here is the frame.cpp file
#include "Frame.h"
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
Frame::Frame()
{
spare = false;
strike = false;
throw1 = 0;
throw2 = 0;
score = 0;
}
//generates a random throw
int Frame::genThrow(int size)
{
int randomNumber = 0;
if (size < 10 || throw1 != 10)
{
randomNumber = 0 + rand() % (11 - throw1); //generate a number between 0 and 10
}
else
{
randomNumber = 0 + rand() % (11);
}
//cout << randomNumber << endl;
return randomNumber;
}
//get first throw
int Frame::getFirstThrow()
{
return throw1;
}
//get second throw
int Frame::getSecondThrow()
{
return throw2;
}
//get the score of both throws
int Frame::getScore()
{
return score;
}
//set the score
void Frame::setScore(int value)
{
score = value;
}
//set the first throw
void Frame::setFirstThrow(int value1)
{
//throw1 = genThrow(value1); //normal generator
//throw1 = 10; //strike game rigged
throw1 = 9; //spare game rigged
}
//set the second throw
void Frame::setSecondThrow(int value2)
{
//throw2 = genThrow(value2); //normal generator
throw2 = 1; //spare game rigged
//throw2 = 10; //strike game rigged
}
//set the strike
void Frame::setStrike(bool value)
{
strike = value;
}
//set the spare
void Frame::setSpare(bool value)
{
spare = value;
}
//get the strike
bool Frame::getStrike()
{
return strike;
}
//get the spare
bool Frame::getSpare()
{
return spare;
}
Here is the bowling.h file
#ifndef BOWLING_H
#define BOWLING_H
#include "Frame.h"
#include<iostream>
using namespace std;
class Bowling
{
private: Frame a[11];
public: void accumulateScore();
void bowl();
void printData();
};
#endif
Here is the bowling.cpp file
#include "Bowling.h"
#include<iostream>
using namespace std;
//takes all of the throw values after bowling and accumulates the correct score
void Bowling::accumulateScore()
{
int totalSum = 0;
for (int x = 0; x < 10; x++)
{
if (a[x].getFirstThrow() + a[x].getSecondThrow() < 10) //not a strike or spare
{
totalSum += a[x].getFirstThrow() + a[x].getSecondThrow();
a[x].setScore(totalSum);
}
else if (a[x].getFirstThrow() == 10) //throws a strike
{
if (x < 9)
{
totalSum += 10 + a[x + 1].getFirstThrow() + a[x + 1].getSecondThrow();
if (a[x + 2].getStrike() == true)
{
totalSum += 10;
}
a[x].setScore(totalSum);
}
}
else if (a[x].getFirstThrow() + a[x].getSecondThrow() == 10) //throws a spare
{
if(x < 10)
{
totalSum += 10 + a[x + 1].getFirstThrow();
a[x].setScore(totalSum);
}
}
}
//player got the 11th frame
if (a[9].getStrike() == true)
{
totalSum += 10 + a[10].getFirstThrow() + a[10].getSecondThrow();
a[9].setScore(totalSum);
}
else if (a[9].getSpare() == true)
{
totalSum += 10;
a[9].setScore(totalSum);
}
}
void Bowling::bowl()
{
//generate all throws and store them in the frames
for (int x = 0; x < 10; x++)
{
a[x].setFirstThrow(x);
if (a[x].getFirstThrow() == 10)
{
a[x].setStrike(true);
}
if (a[x].getStrike() == false)
{
a[x].setSecondThrow(x);
if (a[x].getFirstThrow() + a[x].getSecondThrow() == 10)
{
a[x].setSpare(true);
}
}
a[x].setScore(a[x].getFirstThrow() + a[x].getSecondThrow());
}
//play the 11th frame if they got a strike on the tenth frame
if(a[9].getStrike() == true)
{
a[10].setFirstThrow(10);
if (a[10].getFirstThrow() == 10)
{
a[10].setStrike(true);
}
a[10].setSecondThrow(10);
cout << "The second throw is this value: " << a[10].getSecondThrow() << endl;
if (a[10].getSecondThrow() == 10)
{
a[10].setStrike(true);
}
else if (a[10].getFirstThrow() + a[10].getSecondThrow() == 10)
{
a[10].setSpare(true);
}
a[9].setScore(a[10].getFirstThrow() + a[10].getSecondThrow());
}
}
void Bowling::printData()
{
for (int x = 0; x < 10; x++)
{
cout << "*****************************" << endl;
cout << "Frame " << x + 1 << endl;
cout << "First throw: ";
if (a[x].getStrike() == true)
{
cout << "Strike!" << endl;
}
else
{
cout << a[x].getFirstThrow() << endl;
}
cout << "Second throw: ";
if (a[x].getStrike() == false)
{
if (a[x].getSpare() == true)
{
cout << "Spare!" << endl;
}
else if(a[x].getSpare() == false)
{
cout << a[x].getSecondThrow() << endl;
}
else
{
cout << endl;
}
}
cout << "Score: " << a[x].getScore();
cout << endl;
}
if (a[9].getStrike() == true)
{
cout << "*****************" << endl;
cout << "Frame 11" << endl;
cout << "First throw: ";
if (a[10].getStrike() == true)
{
cout << "Strike!" << endl;
}
else
{
cout << a[10].getFirstThrow() << endl;
}
cout << "Second throw: ";
if (a[10].getStrike() == false)
{
if (a[10].getSpare() == true)
{
cout << "Spare!" << endl;
}
else
{
cout << a[10].getSecondThrow() << endl;
}
}
else
{
cout << "Strike!" << endl;
}
//cout << "Score: " << a[10].getScore();
cout << endl;
}
}
Here is where I test it in main
#include "Bowling.h"
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int dummy = 0;
//create two players that can bowl
Bowling player1;
Bowling player2;
int player1Score = 0;
int player2Score = 0;
//have the players bowl their throws before accumulating score
player1.bowl();
player2.bowl();
//accumulate the score after all of the throws have been done
player1.accumulateScore();
player2.accumulateScore();
//print player 1 data
cout << "Here are the throws and score for the first player: " << endl;
player1.printData();
//spacing
cout << endl << endl;
//print player 2 data
cout << "Here are the throws and score for the second player: " << endl;
player2.printData();
cout << "Enter a dummy number:" << endl;
cin >> dummy;
return 0;
}
I've Made A Program That Makes The Whole Game To Work Perfectly, And The Only Thing I Can't Do Is The Checking For A Win.
I've Made The Game In Two Files:
1. Main
2. Functions.
This Is The 'Main' File:
#include <iostream>
#include <sstream>
#include <windows.h>
#include <string>
using namespace std;
void getname();
void scoregiving();
void gamestart();
void boardmaking();
void fullgameplay();
int main()
{
gamestart();
getname();
scoregiving();
fullgameplay();
}
This Is The Functions File:
#include <iostream>
#include <sstream>
#include <Windows.h>
#include <string>
using namespace std;
string p1, p2;
int tu,
board[6][7],
colomuns[7],
makeboard,
makeboard1,
scoregive,
scoregive1,
input,
colomunss,
check,
check1;
void line()
{
cout << "|=|=|=|=|=|=|=|\n|";
}
void getname()
{
cout << "\n\nPlayer, Please Enter Your Name. You'll Be X\n<< ";
cin >> p1;
cout << "2nd Player, Please Enter Your Name. You'll Be O\n<< ";
cin >> p2;
tu = 1;
}
void gamestart()
{
cout << " (--OOO--OOO---OOO--OOO--)\n";
cout << " |XX========XXX========XX|\n";
cout << " ||CONNECT 4 BY: NETVIZHEN||\n";
cout << " |XX========XXX========XX|\n";
cout << " (--OOO--OOO---OOO--OOO--)";
}
void boardmaking()
{
cout << "\n\nBoard:\n";
cout << "\n 0 1 2 3 4 5 6\n";
line();
for (makeboard = 0; makeboard <= 5; makeboard ++)
for (makeboard1 = 0; makeboard1 <= 6; makeboard1++)
{
if (board[makeboard][makeboard1] == 0)
{
cout << " |";
}
else if (board[makeboard][makeboard1] == 1)
{
cout << "X|";
}
else if (board[makeboard][makeboard1] == 2)
{
cout << "O|";
}
if (makeboard1 == 6)
{
cout << "\n";
line();
}
}
}
void scoregiving()
{
for (scoregive = 0 ; scoregive < 6 ; scoregive++)
for (scoregive1 = 0 ; scoregive1 < 7 ; scoregive1++)
board[scoregive][scoregive1] = 0;
for (colomunss = 0; colomunss <= 6; colomunss++)
colomuns[colomunss] = 0;
}
void wincheck()
{
for (check = 0; check <=5; check++)
for (check1 = 0; check1 <= 6; check1++)
if (board[check][check1] == tu)
if (board[check - 1][check1] == tu && board[check - 2][check1] == tu && board[check - 3][check1] == tu && board[check][check1] == tu)
cout << "ggh";
}
void putin()
{
cout << "\n<< ";
cin >> input;
if (input >= 7)
cout << "\nThis Location Is Outside The Board. Please Retry.";
else if (colomuns[input] > 5 )
cout << "\nThis Column Is Full. Please Retry.";
else
{
board[5-colomuns[input]][input] = tu;
colomuns[input]++;
wincheck();
if (tu == 2)
tu--;
else if (tu == 1)
tu++;
}
}
void fullgameplay()
{
while(true)
{
boardmaking();
putin();
}
}
Is it a tic-tac-toe game ?
But if it is then why row & column are not of same size because it will be a problem to determine the winner if he has his wining streak diagonally.
Brute force: from each position, traverse in the eight different directions as long as the position is in the board, not empty and the same as the original position. If you reach 4, then the player in the original position is the winner.
Since you should do it after each play, at most one player can have a connect 4.
You can make an array of the 8 directions to use the needed deltas for x and y for each direction.
As previously mentioned you would want to check each direction either in 8 for loops or with boolean values to check each time.
bool n = true;
bool ne = true;
bool e = true;
bool se = true;
bool s = true;
bool sw = true;
bool w = true;
bool nw = true;
for (int count = 0; count < 4; count++)
{
if (board[x + count][y] != tu)
{
e = false;
}
if (board[x + count][y + count] != tu)
{
ne = false;
}
//continue for each direction.
if(n == true || ne == true || e == true //and so on)
{
//player wins
}
}
This is the main concept that could be used though obviously some changes will have to be made to fit your code.
So I'm Having A Recursion Bug.
OutPut I Want...
Input 4
* * * *
* * *
* *
*
* *
* * *
* * * *
OutPut I Get..
Input 4
* Big Blank Space*
I Cant Seem To Wrap My Head Very Well Around recursion.
#include<iostream>
#include<fstream>
#include<string>
#include<windows.h>
#include<ctime>
using namespace std;
int i;
bool end = false;
int changer = -1;
int placeHolder;
bool recursionUp(int num1)
{
if(num1 == placeHolder)
{
return true;
}
for(i = placeHolder; i == num1; i--)
{
cout << "*";
}
cout << "\n";
recursionUp(num1 + 1);
}
bool cont = false;
int recursion(int num1)
{
if(num1 == 0)
{
cont = recursionUp(num1);
}
for(i = 1; i <= num1; i++)
{
cout << "*";
}
recursion(num1 - 1);
if(cont)
{
return 0;
}
}
int main()
{
int number;
cout << "Input Star Number...\n";
cout << "\t Input: ";
cin >> number;
placeHolder = number;
recursion(number);
return 0;
}
Can someone point out my mistake?
As with any recursion there are two things you'll need to define:
The end condition
A depth value (and any other information that's needed)
In your case the end condition is when depth == number - 1
You'll want to print before and after each recursion.
void recursion(int depth){
if (depth <= 1) {
cout << '*' << endl << endl;
}else{
for (int i = 0; i < depth; ++i)cout << "* ";
cout << endl << endl;
recursion(depth - 1);
for (int i = 0; i < depth; ++i)cout <<"* ";
cout << endl << endl;
}
}