Practice

In the previous practice session, a function was written to display the game board and code for the first player's move. In this lesson, players will take turns making moves.

Requirements

  • players must take turns
  • before each move, the player should see the message Player {player name} makes a move:
  • record the players' moves
  • after each move, display the game board

Source code

The following code should remain from the previous practice session:
Line 1, Char 1

Implementation

Since the game board has 9 cells, the maximum number of moves will be 9. For this, you can use a for loop:
Line 1, Char 1
Julia moves first, so she will move on even indices, and Dima on odd ones:
Line 1, Char 1
Julia's move is already written; Dima's needs a small change:
Line 1, Char 1

Final code

At the end of the lesson, you should have the following code:
Line 1, Char 1

Exercises

  1. The function PrintBoard(board); is called after each player's move. If you move it to the end of the loop, the logic will not change, and the code will become cleaner. Place it after the else block and verify that the program still works the same way.
Line 1, Char 1
  1. The code for the players' moves is very similar and differs only in the player's name. A great idea is to create another function. Write a function with the signature void PlayerMakesMove(string name), move the player move code into it, make a small change, and verify that the program behavior has not changed. Hint: you can get the first letter of the player's name like this: name.Substring(0, 1).
Line 1, Char 1