First I'd like to say how much I appreciate these forums. They have helped me before, but this is the first time I've posted a question myself.
My problem is that I use a different IDE than my teacher does (probably not the best idea) so I want the program to compile and run properly in his IDE when he tests it.
I use CodeBlocks (although I want to use NetBeans but it's too difficult to set up) and he uses Dev-C++.
So I finished my project in CodeBlocks yesterday and it was working fine. I opened it up in Dev-C++ to test it. I compiled it with Dev-C++ and it compiles, but the program runs differently (incorrectly). The program is a game sort of like tic-tac-toe. Each player takes a turn at placing their "stones". When run from CodeBlocks the game is won when there are 5 of the same "stones" in a row. When run from Dev-C++ the game is won after the first turn, no matter what.
I've looked at my code and can't figure out the problem (since it works fine in CodeBlocks!) so I was hoping someone with more experience could help me understand what is going on.
Thanks. The program is below.
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int n, type, tries;
int getPadding(int n)
{
int max(n*n), padding(0);
while(max > 0)
{
padding++;
max /= 10;
}
return (padding+1);
}
bool displayBoard(int board[ ][50], int n)
{
bool error = 0;
int padding = getPadding(n);
cout << endl;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
switch (board[i][j])
{
case 0:
cout << setw(padding) << n*i+j+1;
break;
case 1:
cout << setw(padding) << "B";
break;
case 2:
cout << setw(padding) << "W";
break;
default:
error = 1;
}
}
cout << endl;
}
cout << endl;
for(int k=(n*getPadding(n)); k>0; k--)
{
cout << "-";
}
return error;
}
bool checkBoard(int board[ ][50], int n, int type)
{
int counterJ(0), counterI(0), counterD(0);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(board[i][j] == type)
{
counterJ++;
}
else
{
counterJ = 0;
}
if(counterJ == 5)
{
return 1;
}
}
}
for(int j=0; j<n; j++)
{
for(int i=0; i<n; i++)
{
if(board[i][j] == type)
{
counterI++;
}
else
{
counterI = 0;
}
if(counterI == 5)
{
return 1;
}
}
}
}
int inputN()
{
int input;
cout << "Please input a value for n: ";
cin >> input;
while (cin.fail() || input < 10 || input > 50) // does not work for inputs such as 15sdak etc.
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Sorry, n must be an integer between 10 and 50.\nPlease input an"
<< " acceptable value: ";
cin >> input;
}
return input;
}
int inputPlace(int n)
{
int input;
cin >> input;
while (cin.fail() || input < 1 || input > (n*n)) // does not work for inputs such as 15sdak etc.
{
tries += 1;
if(tries >= 10)
{
return -1;
}
cin.clear();
cin.ignore(1000, '\n');
cout << "\nYour choice must be an integer between 1 and "
<< (n*n) << ".\nYou have " << (10-tries) << " tries remaining.\n"
<< "Please input an acceptable value: ";
cin >> input;
}
return input;
}
void updatePlace(int board[ ][50], int place, int input, int type)
{
bool error(0);
while (true)
{
if(error)
{
place = inputPlace(input);
if(place == -1)
{
return;
}
}
int i = (place - 1)/input;
int j = place - (i * input) - 1;
if(board[i][j] != 0)
{
tries += 1;
if(tries >= 10)
{
return;
}
error = 1;
cout << "\nThis space is occupied!\nYou have " << (10-tries)
<< " tries remaining.\nPlease insert an acceptable value: ";
}
else
{
board[i][j] = type;
return;
}
}
}
main()
{
bool error(0);
cout << "\n\n\n Welcome to the game of GOBANG!"
<< "\n ------------------------------\n\n"
<< " Player 2 places the white stones. "
<< "Player 1 places the black stones.\n\n"
<< " To start, you must define the size of the board.\n"
<< "\n The board is a square with the dimensions n * n."
<< "\n\n\n";
while (true)
{
int type(1), input(0), place;
int arg[50][50]= {0};
input = inputN();
do
{
switch (type)
{
case 1:
type = 2;
break;
case 2:
type = 1;
break;
default:
error = 1;
}
system("CLS");
cout << "Player " << type << "'s turn:\n\n";
displayBoard(arg, input);
cout << "\n\nWhere would you like to place your stone?: ";
place = inputPlace(input);
if(place != -1)
{
updatePlace(arg, place, input, type);
}
tries = 0;
}
while(checkBoard(arg, input, type) == 0);
system("CLS");
cout << "The winning board!:\n\n";
displayBoard(arg, input);
cout << "\n\nCongradulations! Player " << type << " won!\n\n"
<< "If you would like to play again, enter a new value for n.\n";
}
}