tic tac toe - Tic-tac-toe game with Linked List C++ -


in 1 of c++ books reading, came across 1 exercise proposes me tic-tac-toe game using both linked list , arrays. decided try linked list first, since arrays easier. however, got stuck on how check if won game. here's have far:

struct board {     bool square, circle, empty;     int pos;     board* next; }; void startgame(int first, board* fullboard); board* getboard(board* fullboard);   int main() {     int dice, first;      board* fullboard = null;     cout << "welcome tic-tac-toe dos game. (2 player version)\n\n";     cout << "x player 1 , o player 2.\ni decide starting in first match...\n ";     srand(time(null));     dice = 1;//rand() % 6 + 1;     cout << dice;     if(dice <= 3)     {         first = 1;         cout << "player 1 first!\n";     }     else     {         first = 2;         cout << "player 2 first!\n\n";     }     system("pause");     system("cls");     startgame(first, fullboard); }  void startgame(int first, board* fullboard) {     int choice;     bool isplaying;     for(int = 1; <= 9; i++)         fullboard = getboard(fullboard);      bool isgameon = true;     while(isgameon == true)     {         board* current = fullboard;         while(current != null)         {             if(current->empty == true)                 cout << "   " << current->pos;             else if(current->circle == true)                 cout << "   " << "o";             else                 cout << "   " << "x";             if( current->pos == 4 || current->pos == 7)             {                 cout << "\n";                 cout << "-----------------------\n";             }             else if (current->pos == 1)                 cout << "\n";             else                 cout << "   |";             current = current->next;         }            if(first == 1)         {             isplaying = true;             while(isplaying == true)             {                 cout << "player 1, please put number corresponding area want fill: ";                 cin >> choice;                 while(choice < 1 || choice > 9)                 {                     cout << "invalid choice. please choose valid option: ";                     cin >> choice;                 }                 current = fullboard;                 while(current != null && current->pos != choice)                     current = current->next;                  if(current->empty == true)                 {                     current->empty = false;                     current->square = true;                     isplaying = false;                     first = 2;                 }                 else                     cout << "the field chose used...\n";             }          }         else         {             isplaying = true;             while(isplaying == true)             {                 cout << "player 2, please put number corresponding area want fill: ";                 cin >> choice;                 while(choice < 1 || choice > 9)                 {                     cout << "invalid choice. please choose valid option: ";                     cin >> choice;                 }                 current = fullboard;                 while(current != null && current->pos != choice)                     current = current->next;                  if(current->empty == true)                 {                     current->empty = false;                     current->circle = true;                     isplaying = false;                     first = 1;                 }                 else                     cout << "the field chose used...\n";             }         }          system("cls");     }   }  board* getboard(board* fullboard) {     board* newboard = new board;     newboard->empty = true;     newboard->circle = false;     newboard->square = false;     newboard->next = fullboard;     if(newboard->next != null)         newboard->pos = newboard->next->pos + 1;     else         newboard->pos = 1;     return newboard;   } 

as can see, on struct board, have int called pos, created keep track of whole board. solution can imagine far, checking every single position. ex: compare pos 8 pos 9, 7, 5 , 2, compare pos 9 pos 8, 7, 6, 3, 5 , 1. think way extensive (and maybe it's hard coding well?). other options think have?

thanks in advance, felipe

i need more space explain "multiple lists" concept.

your board:

 _ _ _ |_|_|_| |_|_|_| |_|_|_| 

the lists represent possible solutions

d1  b c  d2     _ _ _  e |_|_|_|  f |_|_|_|  g |_|_|_| 

note each cell in @ least 2 lists.

option 1)

you store lists in single "list of lists". when move done, iterate list of lists, and, each sublist (which a, b, etc. previous figure), iterate cells. if cells in sublist player moved, found winner.

option 2)

each cell has member "list of lists". "list of lists" holds lists check when move done in cell (for example, cell 1, have lists a, e , d1). when move done in cell, list cell , check sublists see if got winner (it more or less same option 1, limiting lists have iterate on each time).

note in cases, dealing lists (if add "pointer diagonal" , similar structure no longer list). that:

  • there many lists.
  • there lists of cells, , lists of lists of cells.

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -