Question about Function Being Always True
Jan 1, 2015 at 7:55am UTC
If you look at my main and in it, the following code:
if(playerOneWin(board))
{
cout << "Player 1 Wins!" << endl;
}
It seems that the program is being run as always true. However, I am not sure exactly why that is the case. Please look at the contents of my playerOneWin() function and if you can figure out why it is being set as always true, that would be awesome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <ctime>
using namespace std;
bool playerOneWin(int board[]){
bool result = true ;
if ((board[0] == 1 || board[2] == 1 || board[4] == 1 || board[6] == 1 || board [8] == 1) &&
(board[0] == 2 || board[2] == 2 || board[4] == 2 || board[6] == 2 || board [8] == 2) &&
(board[0] == 3 || board[2] == 3 || board[4] == 3 || board[6] == 3 || board [8] == 3))
{
cout << "Player 1 Wins!" ;
return result;
}
else if ((board[0] == 4 || board[2] == 4 || board[4] == 4 || board[6] == 4 || board [8] == 4) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 6 || board[2] == 6 || board[4] == 6 || board[6] == 6 || board [8] == 6))
{
cout << "Player 1 Wins!" ;
return result;
}
else if ((board[0] == 7 || board[2] == 7 || board[4] == 7 || board[6] == 7 || board [8] == 7) &&
(board[0] == 8 || board[2] == 8 || board[4] == 8 || board[6] == 8 || board [8] == 8) &&
(board[0] == 9 || board[2] == 9 || board[4] == 9 || board[6] == 9 || board [8] == 9))
{
cout << "Player 1 Wins!" ;
return result;
}
else if ((board[0] == 1 || board[2] == 1 || board[4] == 1 || board[6] == 1 || board [8] == 1) &&
(board[0] == 4 || board[2] == 4 || board[4] == 4 || board[6] == 4 || board [8] == 4) &&
(board[0] == 7 || board[2] == 7 || board[4] == 7 || board[6] == 7 || board [8] == 7))
{
cout << "Player 1 Wins!" ;
return result;
}
else if ((board[0] == 2 || board[2] == 2 || board[4] == 2 || board[6] == 2 || board [8] == 2) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 8 || board[2] == 8 || board[4] == 8 || board[6] == 8 || board [8] == 8))
{
cout << "Player 1 Wins!" ;
return result;
}
else if ((board[0] == 3 || board[2] == 3 || board[4] == 3 || board[6] == 3 || board [8] == 3) &&
(board[0] == 6 || board[2] == 6 || board[4] == 6 || board[6] == 6 || board [8] == 6) &&
(board[0] == 9 || board[2] == 9 || board[4] == 9 || board[6] == 9 || board [8] == 9))
{
cout << "Player 1 Wins!" ;
return result;
}
else if ((board[0] == 1 || board[2] == 1 || board[4] == 1 || board[6] == 1 || board [8] == 1) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 9 || board[2] == 9 || board[4] == 9 || board[6] == 9 || board [8] == 9))
{
cout << "Player 1 Wins!" ;
return result;
}
else if ((board[0] == 3 || board[2] == 3 || board[4] == 3 || board[6] == 3 || board [8] == 3) &&
(board[0] == 5 || board[2] == 5 || board[4] == 5 || board[6] == 5 || board [8] == 5) &&
(board[0] == 7 || board[2] == 7 || board[4] == 7 || board[6] == 7 || board [8] == 7))
{
cout << "Player 1 Wins!" ;
return result;
}
}
int main()
{
int j = 0;
int k = 1;
char numbers[9] = {'1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };
int board[9];
for (int i = 1; i < 10; i++)
{
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 9)
{
cin >> board[j];
j = j + 2;
printBoardPlayerOne(board, numbers);
if (playerOneWin(board))
{
cout << "Player 1 Wins!" << endl;
}
}
if (i == 2 || i == 4 || i == 6 || i == 8)
{
cin >> board[k];
k = k + 2;
printBoardPlayerTwo(board, numbers);
}
}
playerOneWin(board);
playerTwoWin(board);
return 0;
}
Jan 1, 2015 at 1:14pm UTC
Show where playerOneWin is returning false.
Topic archived. No new replies allowed.