Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.
Moderator: Coders of Rage
grim
Chaos Rift Newbie
Posts: 3 Joined: Tue Jun 15, 2010 11:39 pm
Post
by grim » Fri Jun 18, 2010 3:32 am
ok so im making a tic tac toe game, the only issue im having is after a win or draw it will print the winning text but the loop does not stop it continues as if no one had won.
been racking my brain all night as to why... 1 is returned from the checkwin func and draw func if there is a winning/draw condition.
gameP1 and gameP2 just collect the players move.
Code: Select all
void drawGameBoard()
{
char space[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
cout<<"\n\n\n\n -------------\n"<<
" | "<<space[0]<<" | "<<space[1]<<" | "<<space[2]<<" |\n"<<
" -------------\n"<<
" | "<<space[3]<<" | "<<space[4]<<" | "<<space[5]<<" |\n"<<
" -------------\n"<<
" | "<<space[6]<<" | "<<space[7]<<" | "<<space[8]<<" |\n"<<
" -------------\n";
int win = 0;
int count = 0;
while(win < 1){
count++;
cout<<"Player 1, turn "<<count<<endl;
space[9] = gameP1(space);
cout<<"\n\n\n\n -------------\n"<<
" | "<<space[0]<<" | "<<space[1]<<" | "<<space[2]<<" |\n"<<
" -------------\n"<<
" | "<<space[3]<<" | "<<space[4]<<" | "<<space[5]<<" |\n"<<
" -------------\n"<<
" | "<<space[6]<<" | "<<space[7]<<" | "<<space[8]<<" |\n"<<
" -------------\n";
win = checkWinP1(space);
win = checkDraw(space, win);
cout<<"Player 2, turn "<<count<<endl;
space[9] = gameP2(space);
cout<<"\n\n\n\n -------------\n"<<
" | "<<space[0]<<" | "<<space[1]<<" | "<<space[2]<<" |\n"<<
" -------------\n"<<
" | "<<space[3]<<" | "<<space[4]<<" | "<<space[5]<<" |\n"<<
" -------------\n"<<
" | "<<space[6]<<" | "<<space[7]<<" | "<<space[8]<<" |\n"<<
" -------------\n";
win = checkWinP2(space);
}
repeatGame();
}
if more code is needed let me know
noob
Chaos Rift Newbie
Posts: 10 Joined: Wed Oct 29, 2008 1:41 pm
Post
by noob » Fri Jun 18, 2010 9:39 am
I just kind of skimmed over your code, but it looks like the loop is checking the win status of player 2. This is my guess based on not seeing the rest of the code.
dandymcgee
ES Beta Backer
Posts: 4709 Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:
Post
by dandymcgee » Fri Jun 18, 2010 11:12 am
grim wrote:
Code: Select all
win = checkWinP1(space);
win = checkDraw(space, win);
win = checkWinP2(space);
As noob pointed out, you're overwriting the win variable twice, loosing whatever info the first two assignments yielded. Without knowing the return values of any of those functions I can't help any more than that.
Falco Girgis wrote: It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
xiphirx
Chaos Rift Junior
Posts: 324 Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:
Post
by xiphirx » Fri Jun 18, 2010 12:07 pm
As others have pointed out, you are setting the win variable twice. Try using another variable called win2 for the second player and changing your loop conditional to while (win < 1 && win2 < 1)
grim
Chaos Rift Newbie
Posts: 3 Joined: Tue Jun 15, 2010 11:39 pm
Post
by grim » Fri Jun 18, 2010 1:54 pm
thanks for the help, the loop now only goes through once, i keep thinking a wrong return value is being sent but i cant find any. After 2 turns take place the loop automaticly breaks. i included the entire code this time.
Code: Select all
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int checkWinP1(char slots[]);
int checkWinP2(char slots[]);
void drawGameBoard();
char gameP1(char pick[]);
char gameP2(char pick[]);
int checkDraw(char slots[], int win, int win2vc);
void repeatGame();
int main()
{
drawGameBoard();
system("PAUSE");
return 0;
}
void drawGameBoard()
{
char space[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
cout<<"\n\n\n\n -------------\n"<<
" | "<<space[0]<<" | "<<space[1]<<" | "<<space[2]<<" |\n"<<
" -------------\n"<<
" | "<<space[3]<<" | "<<space[4]<<" | "<<space[5]<<" |\n"<<
" -------------\n"<<
" | "<<space[6]<<" | "<<space[7]<<" | "<<space[8]<<" |\n"<<
" -------------\n";
int win = 0;
int win2 = 0;
int draw = 0;
int count = 0;
while(win < 1 && win2 < 1 && draw < 1){
count++;
cout<<"Player 1, turn "<<count<<endl;
space[9] = gameP1(space);
cout<<"\n\n\n\n -------------\n"<<
" | "<<space[0]<<" | "<<space[1]<<" | "<<space[2]<<" |\n"<<
" -------------\n"<<
" | "<<space[3]<<" | "<<space[4]<<" | "<<space[5]<<" |\n"<<
" -------------\n"<<
" | "<<space[6]<<" | "<<space[7]<<" | "<<space[8]<<" |\n"<<
" -------------\n";
win = checkWinP1(space);
draw = checkDraw(space, win, win2);
cout<<"Player 2, turn "<<count<<endl;
space[9] = gameP2(space);
cout<<"\n\n\n\n -------------\n"<<
" | "<<space[0]<<" | "<<space[1]<<" | "<<space[2]<<" |\n"<<
" -------------\n"<<
" | "<<space[3]<<" | "<<space[4]<<" | "<<space[5]<<" |\n"<<
" -------------\n"<<
" | "<<space[6]<<" | "<<space[7]<<" | "<<space[8]<<" |\n"<<
" -------------\n";
win2 = checkWinP2(space);
}
repeatGame();
}
int checkWinP1(char slots[]){
// check for win on verticals
if (slots[0] == 'x' && slots[3] == 'x' && slots[6] == 'x'
|| slots[1] == 'x' && slots[4] == 'x' && slots[7] == 'x'
|| slots[2] == 'x' && slots[5] == 'x' && slots[8] == 'x'){
cout<<"congrats to player1, you won!\n";
return 1;
}
// check for win on horizontal
else if (slots[0] == 'x' && slots[1] == 'x' && slots[2] == 'x'
|| slots[3] == 'x' && slots[4] == 'x' && slots[5] == 'x'
|| slots[6] == 'x' && slots[7] == 'x' && slots[8] == 'x'){
cout<<"congrats to player1, you won!\n";
return 1;
}
// check for win on diagnals
else if (slots[0] == 'x' && slots[4] == 'x' && slots[8] == 'x'
|| slots[2] == 'x' && slots[4] == 'x' && slots[6] == 'x'){
cout<<"congrats to player1, you won!\n";
return 1;
}
else
return 0;
}
int checkWinP2(char slots[]){
// check for win on verticals
if (slots[0] == 'o' && slots[3] == 'o' && slots[6] == 'o'
|| slots[1] == 'o' && slots[4] == 'o' && slots[7] == 'o'
|| slots[2] == 'o' && slots[5] == 'o' && slots[8] == 'o'){
cout<<"congrats to player2, you won!\n";
return 1;
}
// check for win on horizontal
else if (slots[0] == 'o' && slots[1] == 'o' && slots[2] == 'o'
|| slots[3] == 'o' && slots[4] == 'o' && slots[5] == 'o'
|| slots[6] == 'o' && slots[7] == 'o' && slots[8] == 'o'){
cout<<"congrats to player2, you won!\n";
return 1;
}
// check for win on diagnals
else if (slots[0] == 'o' && slots[4] == 'o' && slots[8] == 'o'
|| slots[2] == 'o' && slots[4] == 'o' && slots[6] == 'o'){
cout<<"congrats to player2, you won!\n";
return 1;
}
else
return 0;
}
char gameP1(char pick[]){
//input moves p1
for(char choice = '0';;){
cin>> choice;
if ( choice == '1' && pick[0] != 'x' && pick[0] != 'o'){
pick[0] = 'x';
return pick[0];
}
else if ( choice == '2' && pick[1] != 'x' && pick[1] != 'o'){
pick[1] = 'x';
return pick[1];
}
else if ( choice == '3' && pick[2] != 'x' && pick[2] != 'o'){
pick[2] = 'x';
return pick[2];
}
else if ( choice == '4' && pick[3] != 'x' && pick[3] != 'o'){
pick[3] = 'x';
return pick[3];
}
else if ( choice == '5' && pick[4] != 'x' && pick[4] != 'o'){
pick[4] = 'x';
return pick[4];
}
else if ( choice == '6' && pick[5] != 'x' && pick[5] != 'o'){
pick[5] = 'x';
return pick[5];
}
else if ( choice == '7' && pick[6] != 'x' && pick[6] != 'o'){
pick[6] = 'x';
return pick[6];
}
else if ( choice == '8' && pick[7] != 'x' && pick[7] != 'o'){
pick[7] = 'x';
return pick[7];
}
else if ( choice == '9' && pick[8] != 'x' && pick[8] != 'o'){
pick[8] = 'x';
return pick[8];
}
else{
cout<<"That is not a valid command or that slot is already filled";
}
}
return 0;
}
char gameP2(char pick[]){
//input moves p2
for(char choice = '0';;){
cin>> choice;
if ( choice == '1' && pick[0] != 'x' && pick[0] != 'o'){
pick[0] = 'o';
return pick[0];
}
else if ( choice == '2' && pick[1] != 'x' && pick[1] != 'o'){
pick[1] = 'o';
return pick[1];
}
else if ( choice == '3' && pick[2] != 'x' && pick[2] != 'o'){
pick[2] = 'o';
return pick[2];
}
else if ( choice == '4' && pick[3] != 'x' && pick[3] != 'o'){
pick[3] = 'o';
return pick[3];
}
else if ( choice == '5' && pick[4] != 'x' && pick[4] != 'o'){
pick[4] = 'o';
return pick[4];
}
else if ( choice == '6' && pick[5] != 'x' && pick[5] != 'o'){
pick[5] = 'o';
return pick[5];
}
else if ( choice == '7' && pick[6] != 'x' && pick[6] != 'o'){
pick[6] = 'o';
return pick[6];
}
else if ( choice == '8' && pick[7] != 'x' && pick[7] != 'o'){
pick[7] = 'o';
return pick[7];
}
else if ( choice == '9' && pick[8] != 'x' && pick[8] != 'o'){
pick[8] = 'o';
return pick[8];
}
else{
cout<<"That is not a valid command or that slot is already filled";
}
}
return 0;
}
int checkDraw(char slots[], int win, int win2){
// check for draw
if (win < 1 && win2 < 1
&& slots[0] != '1' && slots[3] != '4' && slots[6] != '7'
|| slots[1] != '2' && slots[4] != '5' && slots[7] != '8'
|| slots[2] != '3' && slots[5] != '6' && slots[8] != '9'){
cout<<"It appears we have a draw!\n";
}
return 1;
}
void repeatGame(){
string repeatPlay;
cout<<"Would you like to play again?\n";
cin>> repeatPlay;
if (repeatPlay == "yes" || repeatPlay == "Yes" || repeatPlay == "YEs" || repeatPlay == "YES",
repeatPlay == "yEs" || repeatPlay == "yeS" || repeatPlay == "yES" || repeatPlay == "YeS")
drawGameBoard();
else if (repeatPlay == "NO" || repeatPlay == "No" || repeatPlay == "no" || repeatPlay == "nO")
cout<<"Thanks for playing\n";
}
Winawer
Chaos Rift Newbie
Posts: 17 Joined: Tue Jan 05, 2010 10:51 am
Post
by Winawer » Fri Jun 18, 2010 2:20 pm
Your checkDraw() function always returns 1.
grim
Chaos Rift Newbie
Posts: 3 Joined: Tue Jun 15, 2010 11:39 pm
Post
by grim » Fri Jun 18, 2010 2:29 pm
Winawer wrote: Your checkDraw() function always returns 1.
wow i feel stupid, thnx lol
EdBoon
Chaos Rift Junior
Posts: 258 Joined: Fri May 28, 2010 10:44 pm
Current Project: Top down multiplayer shooter using unity 3D
Favorite Gaming Platforms: 360, SNES, ps1
Programming Language of Choice: C++, C#
Location: Atlanta, GA
Contact:
Post
by EdBoon » Fri Jun 18, 2010 2:35 pm
grim wrote: Winawer wrote: Your checkDraw() function always returns 1.
wow i feel stupid, thnx lol
lol, don't feel stupid, happens to everyone. Also just a few things to note, if player 1 wins, it still asks player 2 to take a turn before asking if you want to play again. Also, you might want to add an else statement to your repeatGame function incase they dont type exactly yes or no. And if you want, you could put (yes/no) or (y/n) in the question to ask again so they are positive what response to put. Though for a direct question like this its not as important. Good work, Clean code