Apr 15, 2014 at 1:26am UTC
Hi guys,
Im new to linked list so bear with this code. I'm stuck on the winning conditions, and the one I have currently will simply not work if I get x in pos 2,3,4 for the Horizontal check.
Ignore the int data, and me trying to use 'x' or '0' ill fix that later.
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
//4. Implement a two player tic-tac-toe game. First use a linked list to represent the board. Then use
//an array. Which is easier? Why?
typedef struct node* nodePtr;
int playerTurn(1);
list board; // error C2146: syntax error : missing ';' before identifier 'board'
struct node{
int data;
node* next;
};
class list{
public :
nodePtr head;
nodePtr temp;
nodePtr curr;
list();
void move();
void display(nodePtr &temp, nodePtr &head);
//void initialize(nodePtr &temp, nodePtr &head);
void input(nodePtr curr, int searchValue);
void switchTurn();
int whoWon(nodePtr curr);
};
list::list(){
head = NULL;
temp = NULL;
nodePtr n = new node;
nodePtr head = n;
nodePtr temp = n;
nodePtr curr = temp;
n->data = 1;
for (int i = 2; i < 10; i++){
nodePtr localNode = new node;
localNode->data = i;
temp->next = localNode;
temp = localNode;
}
temp->next = NULL;
temp = head;
}
void list::move(){
int input;
board.display(temp, head);
cout << "Please Make a move: " << endl;
cin >> input;
board.input(curr, input);
}
void list::input(nodePtr curr, int searchValue){
if (curr == NULL){
cout << searchValue << " was an invalid move." << endl;
move();
}
else if (curr->data == searchValue){
if (playerTurn == 1){
curr->data = 'x' ;
board.switchTurn();
move();
}else {
curr->data = '0' ;
board.switchTurn();
move();
}
}
else {
input(curr->next, searchValue);
}
}
void list::display(nodePtr &temp, nodePtr &head){
while (temp != NULL){
int i;
for (i = 0; i < 3; i++){
cout << temp->data << "\t" ;
temp = temp->next;
}
cout<<endl;
i = 0;
};
temp = head;
}
//void initialize(nodePtr &temp, nodePtr &head){
//
//}
void switchTurn(){
switch (playerTurn){
case 1:
board.switchTurn();
system("cls" );// refresh board
break ;
case 2:
board.switchTurn();
system("cls" );// refresh board
break ;
}
}
int list::whoWon(nodePtr curr){
int x_Counter(0);
int y_Counter(0);
//HORIZONTAL CHECK
if (curr->data == 'x' ){
y_Counter = 0;
x_Counter++;
whoWon(curr->next);
if (x_Counter == 3){
return 1;
}
}
else if (curr->data == '0' ){
x_Counter = 0;
y_Counter++;
whoWon(curr->next);
if (y_Counter == 3){
return 2;
}
}
else {
return 0;
}
}
int main(){
board.move();
system("pause" );
}
Last edited on Apr 15, 2014 at 1:27am UTC
Apr 15, 2014 at 2:36am UTC
You also declare a variable (list board) line 15 before the declaration of the class list.
error C2146: syntax error : missing ';' before identifier 'board'
That is what it is referring to.
Last edited on Apr 15, 2014 at 2:39am UTC
Apr 16, 2014 at 1:42am UTC
I fixed it, ty.
But i still get errors, forexample my who won function is giving me issues, because I must return a value on the else statements.
Im thinking of putting it in a switch statement with the default, unless there is a cleaner fix for it.
Apr 16, 2014 at 10:05pm UTC
CodeGoggles, that was a left over code, Its supposed to change player turn to 1 or 2 based on the current turn.
Im trying to retun 1, for player 1 won, 2 for 2 won, or 0 for its a tie.
The issue is there because I dont have a return on every else statement such as this block of code:
1 2 3 4 5 6 7 8
if (row_counter <= 3){
if (y_Counter == 3){
return 2;
}
}else {
x_Counter = 0;
y_Counter = 0;
}
the else does not return anything, even if i return whoWon(curr->next) (maybe cuz its an int? not sure.
Im still learning and testing with recursion but this is where I got stuck.
edit:
This is what I have so far but still requires a return on every if else etc.
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
int list::whoWon(nodePtr curr){
int x_Counter(0);
int y_Counter(0);
int row_counter(0);
//HORIZONTAL CHECK
switch (playerTurn){
case 1:
if (curr->data == 'x' ){
y_Counter = 0;
x_Counter++;
row_counter++;
whoWon(curr->next);
if (row_counter <= 3){
if (x_Counter == 3){
return 1;
}
}else {
x_Counter = 0;
y_Counter = 0;
break ;
}
}
case 2:
if (curr->data == '0' ){
x_Counter = 0;
y_Counter++;
row_counter++;
whoWon(curr->next);
if (row_counter <= 3){
if (y_Counter == 3){
return 2;
}
}else {
x_Counter = 0;
y_Counter = 0;
break ;
}
}
default :
return 0;
}
}
Last edited on Apr 17, 2014 at 4:11am UTC