As far as the function that returns the value, I was limited the amount of header files (no particular reason though).
Ignoring that super-function of the demi gods of string-to-in conversions, the issue is occuring WITHIN the BoxNum function.
I discovered that little info after commenting out the specific function and running the debugger. the program goes off flawlessly except when a player inputs a number the placepiece() will always default (which is expected so i assume that works as well)
if you would do me the favor of compileing the source yourself ( assumeing you are useing VBasic c++ express with no altered settings) a blank cmd prompt should pop up but be placced behind the dev-env and a box in the bottom left should show all the defined variables with a + next to playerInput. expand that to see what the error is. essentially is says that the expression cannot be evaluated. My first thought was that one of the if statements were incorrect so i commented them all off one by one and the same thing kept occuring.
you were commenting about how haveing parameter's with names the same as a global variable would affect the program so maybe the issue lies within the parameter's of the BoxNum function and not the Place peice?
note: I typed that whole thing out to help me think more than anything and am now going to make the alterations to the code...
New source:
/* Tic Tac Toe Step By Step
McTOBY
Current issue --BoxNum
Box-Num cannot determine value for playerInput
the first time box-bum is called it will always return a value of 0 for bNum
resolved issue--Switching players
siwtch-case Does NOT either does not change the player
or fucntions do not properly return player id
solution: not sure..either changeing the funtion's arg in def from playerId to PlayerId (dif from var)
possible solution was that function auto-assigned playerId to 0
or adding an additional return playerId; AFTER the switch statement BEFORE the function terminated
function was recieving a value but returned void
*/
#include <iostream>
#include <string>
using namespace std;
// Var's
string playerInput = "1"; //stores player's commands
int playerId = 1; //1 = x 2 = O
int x = 1; //prevents loop from ending -- player may type exit to end program
int bOne, bTwo, bThr, bFor, bFiv, bSix, bSev, bEig, bNin; //boxs 1 = X, 2 = O, 0 = New Game
int bNum; //used to allow player to easily select box number
int winId; // many uses
// ProtoTypes
int SwitchPlayer ( int PlayerId ); // switches between players
int Win( int playerId ); // determines winner
int PlayerInput( string playerInput );// recieves input
int PlacePiece ( int bNum, int PlayerId );// asigns bXxx to a playerId
int BoxNum ( string playerInput );// Converts playerInput to int to assign to bXxx
void ShowGrid ( void ); //displays the current grid
int main()
{
do {
cin >> playerInput;
PlayerInput ( playerInput );
if ( playerInput == "exit" )//ends program
{
break;
}
SwitchPlayer ( playerId );
cout << playerId <<endl;
} while (x != 0);
return 0;
}
int SwitchPlayer ( int PlayerId )
{
switch ( playerId ) {
case 1:
playerId = 2;
return playerId;
break;
case 2:
playerId = 1;
return playerId;
break;
default:
cout << "defaulted" <<endl;
return playerId;
}
return playerId;
}
int CheckWin( int playerId )
{
if ( playerId == 1 )
{
winId = playerId;
cout << "player " << winId << " WINS!" <<endl;
} else if ( playerId == 2 )
{
winId = playerId;
cout << "player " << winId << " WINS!" <<endl;
}
return winId;
}
int PlayerInput( string playerInput )
{
if ( playerInput == "check win" )
{
CheckWin( playerId );
} else if ( playerInput == "p1" )
{
playerId = 1;
} else if ( playerInput == "p2" )
{
playerId = 2;
} else if ( playerInput == "extra" )
{
cin >> playerInput;
PlayerInput ( playerInput );
cin >> playerInput;
PlayerInput ( playerInput );
} else if ( playerInput == "1" || playerInput == "2" || playerInput == "3" || playerInput == "4" || playerInput == "5" || playerInput == "6" || playerInput == "7" || playerInput == "8" || playerInput == "9" )
{
PlacePiece ( bNum, playerId );
} else if ( playerInput == "show" )
{
ShowGrid();
}
return playerId;
}
int PlacePiece ( int bnum, int PlayerId )
{
switch ( BoxNum ( playerInput ) )
{
case 1:
bOne = playerId;
return bOne;
break;
case 2:
bTwo = playerId;
return bTwo;
break;
case 3:
bThr = playerId;
return bThr;
break;
case 4:
bFor = playerId;
return bFor;
break;
case 5:
bFiv = playerId;
return bFiv;
break;
case 6:
bSix = playerId;
return bSix;
break;
case 7:
bSev = playerId;
return bSev;
break;
case 8:
bEig = playerId;
return bEig;
break;
case 9:
bNin = playerId;
return bNin;
break;
default:
cout << "error, no piece placed!" <<endl;
return playerId;
break;
}
}
int BoxNum ( string playersInput )
{
if ( playerInput == "1" )
{
bNum = 1;
} else if ( playerInput == "2" )
{
bNum = 2;
} else if ( playerInput == "3" )
{
bNum = 3;
} else if ( playerInput == "4" )
{
bNum = 4;
} else if ( playerInput == "5" )
{
bNum = 5;
} else if ( playerInput == "6" )
{
bNum = 6;
} else if ( playerInput == "7" )
{
bNum = 7;
} else if ( playerInput == "8" )
{
bNum = 8;
} else if ( playerInput == "9" )
{
bNum = 9;
} else {
cout << "defaulted" <<endl;
}
return bNum;
}
void ShowGrid ( void )
{
cout << bOne << bTwo << bThr <<endl;
cout << bFor << bFiv << bSix <<endl;
cout << bSev << bEig << bNin <<endl;
}
Main differences, The param in BoxNum changed from playerInput (glabal var name) to playersInput
also added a ShowGrid function to show the alterations to the vars used to store which player placed their pieces and where. To show the grid i added if-then into the PlayerInput() so that by typeing "show" you can see the alterations.
The change to the boxNum did NOT remove the warning from the compiler however when the program is run the expressions are (at least as far as i tested) evaluated correctly.
A final not that i would like to add is that if i only read my comment up at the top of the source outlineing the last issue in which a function apeared to not be reciveing proper input was solved in an identicle way i could have avoided this whole thread and for that i aopologize.
However if i have any other issues with this particular program this thread is where i will post them.
Thank you for your assistance!!!
edit:::
the compiler quit displaying the error after changeing the param name in PlayerInput() from playerInput to playerzInput
i added a few lines of code so far with easily fixed minor errors
as for now i only need features anyone who happens to read this thread may want to see in a game like this, so far i have a small todo list mostly to complete a v1.0 of the game for people to play with themselves if / when they get bored enough
[edit] updated todo-list
Done: find a way to clear screen each time the main loop is run to prevent clutter
Done: add admin status check to the player input commands used for debugging
Done: impliment a NewGame function to reset the var's so that a new game can be started without restarting the app
Done: impliment a settings enviroment where players can alter settings
Done: add a function to allow player(s) to decide if they want the grid to corospond with the NumPad (ie bOne would be botton
instead of top left)
Done: alter the Win function to determine a winner--does not determine cats games or 3-5-7 wins, cat command added in such events
ToDo: impliment a function to keep tracks of win's and loss's for x's and o's
ToDo: expand function to keep track of individual players' scores ( function will allow for players to alternate between x and o each game
ToDo: impliment a.i. (will be VERY last thing i do due to the comples switch-case's involved)
Done: alter place-piece to prevent player's from takeing eachother's squares
ToDo: come up with more ToDo's
if you have any suggestions lemme know
[edit] v1.0 done! you can now challenge yourself! and yourself! and any lonely friends who have nothing better to do! (like me!!(hint hint: i need friends 8-( )).
[edit] it has come to my attention that the .exe you can download does not load... i will work on the issue if oyu feel like trying it go ahead..
[edit] the issue is that the program requires the vc+= runtime env or something... I am going to begin developing (at least to create a useable .exe) with devc++ until i am familliar with the win32 api (so that my friends can assist in debugging without annoying downloads)
after dealing with that issue and realizeing that i have approximately 360800 games to code in order to create and impossible ai that will be last, my next step is to take it out of the console and (using the win32 api) add a GUI.. after becomeing familliar with the win32 api i will expand it further incorperating the direct x 9.0c SDK and than and only than will i bring it into the darkGDK ( i understand that darkGDK is much simpler to learn however the rout i'm takeing should provide more experience and understanding )
[edit] http://picasoe.iwarp.com has the working game as well as some stuff i made to help me learn (notes and sources of my own with ALOT of comments)
EYE R T3H +ick +ack +oe mester