Restrictions

  • No global variables may be used
  • Your main function may only declare variables and call other functions

Description

Write a program to implement the game connect-n. Connect-n is like Connect-4 but the size of the board the number of pieces in a row needed to win are user parameters. If you have never played Connect-4 before you can play it here: https://www.mathsisfun.com/games/connect4.html. The basic gist of the game is that each player takes a turn dropping one of their pieces into a column. Pieces land on top of pieces already played in that column. Each player is trying to get n pieces in a row either veritcally, horizontally or diagonally. The game ends if either player gets n pieces in a row or the board becomes full.

Additional Details

  • Your program should accept 3 command line parameters:
    • The number of rows on the board
    • The number of columns on the board
    • The number of pieces in a row needed to win
  • It is ok if the number of pieces in a row is larger than what can fit on a board
  • For example on a 3 X 3 board it is ok for the number of pieces in a row needed to win to be 100
  • If the user does not enter enough arguements, too many arguements or invalid values your program should show the user how to call your program and then terminate
    • You may find exit here
  • Player 1's pieces are represented by X
  • Player 2's pieces are represented by O
  • Player 1 always plays first
  • If the user enters an invalid play your program should continue to ask them for input until valid input is entered
  • After the game is over a winner should be declared if there is one and if there is no winner a tie should be declared
  • You must split your code into at least 2 different .c files

Assumptions

  • Input will not always be valid
    • If invalid input is entered on the command line your program should show the user how to use the program and then quit
    • If invalid input is entered when the program is running your program should continue to ask for input until valid input is entered

Valid Input

  • Number of rows: An integer greater than 0
  • Number of columns: An integer greater than 0
  • Number of pieces in a row needed to win: An integer greater than 0
  • User Move: An integer specifying a column between 0 and the number of columns - 1 that is not already full

Hints

  • This is our first large program. It took me about 300 lines of code to complete it
    • You will want to break your problem down into logical steps before beginning on it.
    • Each of these steps will become a function
    • Each of these steps might have steps within them so you should create functions here as well to help break down the problem even farther
    • Once you get to small enough step go ahead and solve it
  • Here are some of the functions I had in my program
    • read_args
    • create_board, print_board, destroy_board
    • play_game, get_play, play_is_valid
    • game_over, game_won, row_win, col_win, diag_win, right_diag_win, left_diag_win

Examples

User input has been underlined to help you differentiate between what the user is entering and what the program is ouputting. You do not need to underline anything.

Example 1

./connectn.out
Not enough arguments entered
Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win

Example 2

./connectn.out 1 2 3 4 5
Too many arguments entered
Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win

Example 3

./connectn.out 3 3 3
2 * * *
1 * * *
0 * * *
0 1 2
Enter a column between 0 and 2 to play in: 0
2 * * *
1 * * *
0 X * *
0 1 2
Enter a column between 0 and 2 to play in: 1
2 * * *
1 * * *
0 X O *
0 1 2
Enter a column between 0 and 2 to play in: 0
2 * * *
1 X * *
0 X O *
0 1 2
Enter a column between 0 and 2 to play in: 0
2 O * *
1 X * *
0 X O *
0 1 2
Enter a column between 0 and 2 to play in: 0
Enter a column between 0 and 2 to play in: 0
Enter a column between 0 and 2 to play in: -2
Enter a column between 0 and 2 to play in: 4
Enter a column between 0 and 2 to play in: 1
2 O * *
1 X X *
0 X O *
0 1 2
Enter a column between 0 and 2 to play in: 2
2 O * *
1 X X *
0 X O O
0 1 2
Enter a column between 0 and 2 to play in: 2
2 O * *
1 X X X
0 X O O
0 1 2
Player 1 Won!

Example 4

./connectn.out 1 2 3
0 * *
0 1
Enter a column between 0 and 1 to play in: 0
0 X *
0 1
Enter a column between 0 and 1 to play in: 1
0 X O
0 1
Tie game!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.