Skip to content

Latest commit

 

History

History
51 lines (46 loc) · 3.1 KB

README.md

File metadata and controls

51 lines (46 loc) · 3.1 KB

Tic-tac-toe

A mini game tic-tac-toe implemented in C.

Problem statement

To ask players to input tokens on the plate using the numeric keys. Since the 9 squares on the plate are marked with integers from 1 to 9 respectively, players need to input tokens by tapping numeric keys 1 - 9. The program should judge that player 1 or player 2 have won the game when 3 tokens with the same type are in a line. And the program should consider it as a draw when the 9 squares are filled with tokens but without the 3 same type tokens in a line. Besides, illegal inputs should be detected and reported, which are any inputs other than the integer numbers from 1 to 9. Repeated inputs are also considered as illegal and will be reported if input.

Analysis

(Assume that the user only inputs valid data.)

Inputs

Integer numbers from 1 to 9

Outputs

Two kinds of tokens O and X in 9 squares separately

Additional requirements or constraints

The same number cannot be chosen repeatedly

Design

  1. Define 2 variables: row and column
    row is assigned as 7 and it represents the numbers in a single row.
    column is assigned as 3 and it represents the numbers in a single column.
  2. Declare a series of functions and variables:
    char p [row] [column] means declaring a 2D array of row*column.
    int board(char p [row] [column]) means that the 2D array should be inserted into an assigned integer as the game board.
    char order (int a) means that an integer a should be inserted into a character order.
    int judge(char p [row] [column]) means that the 2D array should be inserted into an integer as a judgment basis.
    int sum = 1 represents an integer assigned as 1.
    int operate(char p [row] [column], int sum) means declaring a function operate with elements char p row column and int sum.
    int x represents an integer x, which refers to a number in a single row, or the number of columns.
    int y represents an integer y, which refers to a number in a single column, or the number of rows.
    char s = 48 means assigning a character s as 48 according to ASCLL, which equals to 0 decimal.
    int ch represents an integer ch.
    int num = 0 means that the integer is assigned as 0.
  3. Design an opening interface of the game.
  4. Build a single judging loop to guide players to start the game and lead to the main interface of the game.
  5. Print out the game board in the main interface using defined 2D array.
  6. Define the game rules about placing the tokens.
  7. Evaluate the logical judgments to define whether the game is a win, a lose or a draw.
  8. Detect and report if there is an illegal input.

Demonstration

Gameboard

Image

'O' wins

Image

'X' wins

Image

Draw

Image

Illegal Input

Image