Skip to content

Instantly share code, notes, and snippets.

@yangshun
Last active April 28, 2019 19:53
Show Gist options
  • Save yangshun/8f102cc90771056f20ccee942783e1f5 to your computer and use it in GitHub Desktop.
Save yangshun/8f102cc90771056f20ccee942783e1f5 to your computer and use it in GitHub Desktop.

Connect 4

Connect 4 is a game where two players aim to connect up their pieces to make 4 in a row. Each player takes turn to choose a column to drop their pieces. The first player to connect 4 pieces in a row wins the game. Player 1's pieces are represented by X and Player 2 by O. The game will start with player 1 first. The board dimensions is 6 x 7. Your cli game will have to prompt the user to enter the column number (1-based) that they want to place the next piece in.

.......
.......
.......
.......
.......
.......

Enter player 1's move: 4

.......
.......
.......
.......
.......
...X...

Enter player 2's move: 5

.......
.......
.......
.......
.......
...XO..

Enter player 1's move: 5

.......
.......
.......
.......
....X..
...XO..

Enter player 2's move:

When a winner has been determined, print the message Player X wins! and allow the user to start a new game or exit the program.

Maze

You are given a map of N x M size. Walls are denoted by #, the player is denoted by X, exit denoted by @ and walkable paths are denoted by . Each turn, your game will prompt the user to enter a direction: up, down, left, right. There are a limited number of moves the player can make, and if the player uses up all the moves before reaching the exit, he is deemed to have lost. There is guaranteed to be an exit. If the player enters a direction that is not possible to walk to (such as trying to walk into a wall), that is counted as one move.

The input format is: <N> <M> <lives> followed by the map. An example input is shown below:

6 7 12
#######
#.#...#
#...#.@
#.#####
#...X.#
#######

Enter direction to move: left

#######
#.#...#
#...#.@
#.#####
#..X..#
#######

Number of moves left: 11
Enter direction to move: up

#######
#.#...#
#...#.@
#.#####
#..X..#
#######

Number of moves left: 10
Enter direction to move: asd
Please enter a valid direction!
Enter direction to move: left

#######
#.#...#
#...#.@
#.#####
#.X...#
#######

Number of moves left: 9
Enter direction to move: ...

Other possible grid-based cli games:

  • Game of Life (it's also an interview question!)
  • Reversi
  • Go
  • Battleship
  • Minesweeper
  • Tetris -gulps-
  • Sudoku
  • 2048!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment