Thread: Question about restarting a loop

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    34

    Question about restarting a loop

    I finished my ttt game, its version 1.0. I decided that once somebody has won I want to ask them if they want to play again. How would you go back into a loop once you left it? Here is my code:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int turn = 1, move = 0, done = 0; // Sets turn to 1, move to 0, and done to 0
    char restart, y, Y;
    char name1[25], name2[25];
    char board[9]; // The tic-tac-toe Board
    
    void DrawBoard();
    void DrawBoard() //* Draws the board *//
    {
    printf("\n\n");
    printf(" %c | %c | %c \n", board[0], board[1], board[2]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[3], board[4], board[5]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[6], board[7], board[8]);
    printf("\n\n");
    }// DrawBoard
    
    void Wincheck()
    {
     if ((board[0] == 'X' && board[1] == 'X' && board[2] == 'X') ||
          (board[3] == 'X' && board[4] == 'X' && board[5] == 'X') ||
          (board[6] == 'X' && board[7] == 'X' && board[8] == 'X') || 
          (board[0] == 'X' && board[3] == 'X' && board[6] == 'X') || 
          (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') || 
          (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') || 
          (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') || 
          (board[2] == 'X' && board[4] == 'X' && board[6] == 'X'))
     {printf("%s wins\n", name1);
    	done = 1;}
    
      if ((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') ||
          (board[3] == 'O' && board[4] == 'O' && board[5] == 'O') ||
          (board[6] == 'O' && board[7] == 'O' && board[8] == 'O') || 
          (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') || 
          (board[1] == 'O' && board[4] == 'O' && board[7] == 'O') || 
          (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') || 
          (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') || 
          (board[2] == 'O' && board[4] == 'O' && board[6] == 'O'))
      {printf("%s wins\n", name2);
      done = 1;}
    }
    
    void Playersturn();
    void Playersturn() // Players turn
    {
    
    // player 1
    	if(turn == 1)
    		{printf("It is %s's turn: ", name1);
    		scanf("%d", &move);
    		move--;
    		if( (board[move]!='X')&&(board[move]!='O') )
    			{board[move]='X';turn=2;}
    		else 
    			printf("Invalid Move\n");
    			system("cls");
    							
    }// player 1
    
    // player 2
    	else
    		{printf("It is %s's turn: ", name2);
    		scanf("%d", &move);
    		move--;
    		if( (board[move]!='X')&&(board[move]!='O') )
    			{board[move]='O';turn=1;}
    		else 
    			printf("Invalid move\n");
    			system("cls");
    		
    	} //player 2
    } //players turn
    
    int main()
    {
    
    printf("\n");
    printf("Welcome to tic-tac-toe!!!\n\n");
    
    // Players names
    printf("What is 1st player's name: ");	
    	scanf("%s", name1);
    printf("\nWhat is 2nd player's name: ");
    	scanf("%s", name2);
    system("cls");
    
    do
    	{
    	
    	DrawBoard();
    	Playersturn();
    	Wincheck();
    
    	} // do loop
    while (done == 0);
    
    return 0;	
    
    }// int main()

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    looks good. Let me see what it does:-D

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    I have been busy working and stuff... I got this done in my spare time.. Its version 1.2:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    
    int turn = 1, move = 0, done = 0; // Sets turn to 1, move to 0, and done to 0
    
    char restart;
    char name1[25], name2[25];
    char board[9] = {'1','2','3','4','5','6','7','8','9'}; // The tic-tac-toe Board
    
    void Playersnames()
    {
    // Players names
    printf("What is 1st player's name: ");	
    	scanf("%s", name1);
    printf("\nWhat is 2nd player's name: ");
    	scanf("%s", name2);
    system("cls");
    }
    
    void DrawBoard()
    {
    printf("\n\n");
    printf(" %c | %c | %c \n", board[0], board[1], board[2]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[3], board[4], board[5]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[6], board[7], board[8]);
    printf("\n\n");
    }// DrawBoard
    
    void Playersturn()
    {
    	if(turn == 1)
    		{printf("It is %s's turn: ", name1);
    		scanf("%d", &move);
    		move--;
    		if((board[move]!='X')&&(board[move]!='O'))
    			{board[move]='X';turn=2;}
    		else
    			{printf("Invalid Move\n");}
    		system("cls");										
    }// player 1
    
    	else
    		{printf("It is %s's turn: ", name2);
    		scanf("%d", &move);
    		move--;
    		if((board[move]!='X')&&(board[move]!='O'))	
    			{board[move]='O';turn=1;}
    		else 
    			{printf("Invalid move\n");}
    		system("cls");		
    } //player 2
    
    } //players turn
    
    void Wincheck()
    {
    	if ((board[0] == 'X' && board[1] == 'X' && board[2] == 'X') ||
    		(board[3] == 'X' && board[4] == 'X' && board[5] == 'X') ||
    		(board[6] == 'X' && board[7] == 'X' && board[8] == 'X') || 
    		(board[0] == 'X' && board[3] == 'X' && board[6] == 'X') || 
    		(board[1] == 'X' && board[4] == 'X' && board[7] == 'X') || 
    		(board[2] == 'X' && board[5] == 'X' && board[8] == 'X') || 
    		(board[0] == 'X' && board[4] == 'X' && board[8] == 'X') || 
    		(board[2] == 'X' && board[4] == 'X' && board[6] == 'X'))
    	{printf("%s wins!!!\n", name1);
    	done = 1;}
    
    	if ((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') ||
    		(board[3] == 'O' && board[4] == 'O' && board[5] == 'O') ||
    		(board[6] == 'O' && board[7] == 'O' && board[8] == 'O') || 
    		(board[0] == 'O' && board[3] == 'O' && board[6] == 'O') || 
    		(board[1] == 'O' && board[4] == 'O' && board[7] == 'O') || 
    		(board[2] == 'O' && board[5] == 'O' && board[8] == 'O') || 
    		(board[0] == 'O' && board[4] == 'O' && board[8] == 'O') || 
    		(board[2] == 'O' && board[4] == 'O' && board[6] == 'O'))
    	{printf("%s wins!!!\n", name2);
    	done = 1;}
    
    }
    
    void Restart() // Clears and resets the board and done
    { 
    	memset(board, 0, 9);
    	done = 0;
    	system("cls");
    }
    
    void Playgame ( void ) 
    {
        do
    	{
    	    DrawBoard();
    	    Playersturn();
    	    Wincheck();
    	} // playgame loop
        while (done == 0);
    }
    
    int main()
    {
        printf("\n");
        printf("Welcome to Tic-Tac-Toe!!!\n\n");
    	Playersnames();
    
    do 
    { 
    	Playgame();
    
    printf("Do you want to play again?");
    	restart=getch();
    	Restart();
    } // Do loop
    while( toupper(restart) == 'Y');
    
    return 0; 
    }// Int main()
    The one thing you noticed, if you ran the code, was once you restarted it doesn't have the numbers on the board. Any ideas on fixing that

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    In Restart(), you memset the board to 0s. You'll have to set it up again with something like:


    Code:
    void SetupBoard()
    {
      int i;
    
      for(i = 0; i<9; i++)
        board[i] = i + 1;
    }

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Thanks froggy... that is what I thought i had to do

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    Oops. I just realized that you were using chars. The code I gave you won't work correctly. Try this..


    Code:
    void SetupBoard()
    {
      int i;
    
      for(i = 0; i < 9; i++)
        board[i] = '1' + i;
    }

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Yea I saw that..... Thanks

    New problem... numbers don't want to change to 'X' or 'O' with that new code???

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    161
    If it's still giving you a problem, repost all of the new code.

    By the way.. what part of Florida are you from?

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Alright here it is:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    
    int turn = 1, move = 0, done = 0; // Sets turn to 1, move to 0, and done to 0
    
    char restart;
    char name1[25], name2[25];
    char board[9]; // The tic-tac-toe Board
    
    void Playersnames()
    {
    // Players names
    printf("What is 1st player's name: ");	
    	scanf("%s", name1);
    printf("\nWhat is 2nd player's name: ");
    	scanf("%s", name2);
    system("cls");
    }
    
    void DrawBoard()
    {
    int n;
    for(n=0;n<9;n++) {board[n]= '1' + n;}
    printf("\n\n");
    printf(" %c | %c | %c \n", board[0], board[1], board[2]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[3], board[4], board[5]);
    printf(" --------- \n");
    printf(" %c | %c | %c \n", board[6], board[7], board[8]);
    printf("\n\n");
    }// DrawBoard
    
    void Playersturn() // Players turn
    {
    
    // player 1
    	if(turn == 1)
    		{printf("It is %s's turn: ", name1);
    		scanf("%d", &move);
    		move--;
    		if( (board[move]!='X')&&(board[move]!='O') )
    			{board[move]='X';turn=2;}
    		else 
    			printf("Invalid Move\n");
    			system("cls");
    							
    }// player 1
    
    // player 2
    	else
    		{printf("It is %s's turn: ", name2);
    		scanf("%d", &move);
    		move--;
    		if( (board[move]!='X')&&(board[move]!='O') )	
    			{board[move]='O';turn=1;}
    		else 
    			printf("Invalid move\n");
    			system("cls");
    		
    	} //player 2
    } //players turn
    
    void Wincheck()
    {
     if ((board[0] == 'X' && board[1] == 'X' && board[2] == 'X') ||
          (board[3] == 'X' && board[4] == 'X' && board[5] == 'X') ||
          (board[6] == 'X' && board[7] == 'X' && board[8] == 'X') || 
          (board[0] == 'X' && board[3] == 'X' && board[6] == 'X') || 
          (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') || 
          (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') || 
          (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') || 
          (board[2] == 'X' && board[4] == 'X' && board[6] == 'X'))
     {printf("%s wins\n", name1);
    	done = 1;}
    
      if ((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') ||
          (board[3] == 'O' && board[4] == 'O' && board[5] == 'O') ||
          (board[6] == 'O' && board[7] == 'O' && board[8] == 'O') || 
          (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') || 
          (board[1] == 'O' && board[4] == 'O' && board[7] == 'O') || 
          (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') || 
          (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') || 
          (board[2] == 'O' && board[4] == 'O' && board[6] == 'O'))
      {printf("%s wins\n", name2);
      done = 1;}
    }
    
    void Playgame() 
    {
        do
    	{
    	    DrawBoard();
    	    Playersturn();
    	    Wincheck();
    	} // playgame loop
        while (done == 0);
    }
    
    void Clearall()
    {
    	memset((void *)board, 0, sizeof(board));
    	done = 0;
    }
    
    int main()
    {
    
    printf("\n");
    printf("Welcome to tic-tac-toe!!!\n\n");
    Playersnames();
    do 
    { 
    Playgame();
    printf("Do you want to play again?\n");
    restart=getch();
    Clearall ();
    system("cls"); 
    }
    
    while( toupper(restart) == 'Y');
    
    return 0; 
    }// int main()
    I think whats wrong is that when the function to draw the board is called I think it resets the board.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Yeah that is what it was... I fixed it !!! Now I am going to clean up the code and see if I can delete some lines...
    Last edited by librab103; 07-19-2003 at 12:05 AM.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Question of the day is... I want to make an if statement that if you press a letter besides a number it wont crash the program... Something like this:
    Code:
    if ( move >a || <z)
    { } //do this
    else
    {} //do this
    Would this work ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM