Tic-Tac-Toe : Array Char « Data Type « C / ANSI-C

Home
C / ANSI-C
1.assert.h
2.Console
3.ctype.h
4.Data Structure Algorithm
5.Data Type
6.Development
7.File
8.Function
9.Language Basics
10.Macro Preprocessor
11.Math
12.math.h
13.Memory
14.Pointer
15.setjmp.h
16.signal.h
17.Small Application
18.stdio.h
19.stdlib.h
20.String
21.string.h
22.Structure
23.time.h
24.wctype.h
C Tutorial
C++
C++ Tutorial
Visual C++ .NET
C / ANSI-C » Data Type » Array CharScreenshots 
Tic-Tac-Toe
Tic-Tac-Toe

#include <stdio.h>

void main()
{
  int i = 0;                                   /* Loop counter                         */
  int player = 0;                              /* Player number - 1 or 2               */
  int go = 0;                                  /* Square selection number for turn     */
  int row = 0;                                 /* Row index for a square               */  
  int column = 0;                              /* Column index for a square            */
  int line = 0;                                /* Row or column index in checking loop */
  int winner = 0;                              /* The winning player                   */
  char board[3][3{                         /* The board                            */
                       {'1','2','3'},          /* Initial values are reference numbers */
                       {'4','5','6'},          /* used to select a vacant square for   */
                       {'7','8','9'}           /* a turn.                              */
                     };

   /* The main game loop. The game continues for up to 9 turns */
   /* As long as there is no winner                            */
   fori = 0; i<&& winner==0; i++)
   {
      /* Display the board */
    printf("\n\n");
      printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
      
      player = i%1;                           /* Select player */
 
      /* Get valid player square selection */
      do
      {
         printf("\nPlayer %d, please enter the number of the square "
       "where you want to place your %c: ", player,(player==1)?'X':'O');
         scanf("%d", &go);

         row = --go/3;                                 /* Get row index of square      */
         column = go%3;                                /* Get column index of square   */
      }while(go<|| go>|| board[row][column]>'9');

      board[row][column(player == 1'X' 'O';        /* Insert player symbol   */

      /* Check for a winning line - diagonals first */     
      if((board[0][0== board[1][1&& board[0][0== board[2][2]) ||
         (board[0][2== board[1][1&& board[0][2== board[2][0]))
        winner = player;
      else
      /* Check rows and columns for a winning line */
        for(line = 0; line <= 2; line ++)
          if((board[line][0== board[line][1&& board[line][0== board[line][2])||
             (board[0][line== board[1][line&& board[0][line== board[2][line]))
            winner = player;
      

   }
   /* Game is over so display the final board */
   printf("\n\n");
   printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
   printf("---+---+---\n");
   printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
   printf("---+---+---\n");
   printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

   /* Display result message */
   if(winner == 0)
      printf("\nHow boring, it is a draw\n");
   else
      printf("\nCongratulations, player %d, YOU ARE THE WINNER!\n", winner);
}


           
       
Related examples in the same category
1.Compute the total of a list of numbers
2.Output the address of char array
3.For loop a char array
4.A simple dictionaryA simple dictionary
5.For loop a char array using pointer
6.Char array: pointer and loop
7.Assign a value to an element inside a char array
8.Char array: assign value and loopChar array: assign value and loop
9.Loop and output a char array
10.how array addressing and pointer arithmetic are linkedhow array addressing and pointer arithmetic are linked
11.Show how bitmapped graphics may be usedShow how bitmapped graphics may be used
12.the use of strings
13.contiguous array storage
14.verifying array initialization
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.