Skip to content

Testing a Computer Player

Rainer Stropek edited this page Oct 2, 2020 · 1 revision

How to Test a Computer Player

Introduction

This wiki pages describes how you can test a computer player that you created.

Unit Tests

As a developer, you should write unit tests for your player. You are encouraged to do that for your players. Put your unit tests into the NBattleshipCodingContest.Players.Tests project.

You can find a simple demo player that sequentially shoots at each square in Sequential.cs. Look at some demo tests for this player in SequentialTests.cs.

Debugging

Of course you can debug your unit tests as usual.

If you want to debug a match between two players, run a console game using the command line parameters consolegame -p <player 1 index> -q <player 2 index>. You can find out the player indexes by starting the battleship game with the parameter players. The following console output demonstrates the process.

  • First, we get the player indexes
  • Next, we trigger a game between two players
  • As a result, we see the boards at the end of the game and we get notified who was the winner
  • Of course, you can run the program in the debugger and set breakpoints in your computer player implementation
C:\Code\GitHub\NBattleshipCodingContest\NBattleshipCodingContest>dotnet run -- players
Here is a list of players:

0: global::NBattleshipCodingContest.Players.RandomShots
1: global::NBattleshipCodingContest.Players.Sequential
2: global::NBattleshipCodingContest.Players.SmartRandomShots

C:\Code\GitHub\NBattleshipCodingContest\NBattleshipCodingContest>dotnet run -- consolegame -p 0 -q 1
Player 2 is the winner
Shooting board of player 1
┏━━┯━━┯━━┯━━┯━━┯━━┯━━┯━━┯━━┯━━┓
┃~~│~~│  │  │XX│  │~~│~~│  │~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│  │~~│~~│XX│  │XX│~~│XX│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│XX│  │~~│XX│~~│  │~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃  │  │  │~~│~~│~~│XX│~~│  │~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│XX│~~│~~│~~│~~│XX│  │  │~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│XX│~~│  │  │~~│~~│  │~~│  ┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃  │XX│~~│~~│  │  │~~│~~│  │~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃  │  │  │  │~~│~~│~~│~~│~~│  ┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│  │~~│  │  │~~│~~│  │~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃  │  │~~│XX│  │XX│~~│  │  │~~┃
┗━━┷━━┷━━┷━━┷━━┷━━┷━━┷━━┷━━┷━━┛

Shooting board of player 2
┏━━┯━━┯━━┯━━┯━━┯━━┯━━┯━━┯━━┯━━┓
┃~~│~~│~~│~~│~~│~~│~~│~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃XX│XX│XX│XX│XX│~~│XX│~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│~~│~~│~~│~~│XX│~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│~~│~~│~~│~~│XX│~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│~~│~~│~~│~~│XX│~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│~~│~~│~~│~~│~~│~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│XX│XX│XX│~~│~~│~~│~~│XX┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│~~│~~│~~│~~│~~│~~│~~│XX┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│~~│~~│~~│~~│~~│~~│~~│~~┃
┠──┼──┼──┼──┼──┼──┼──┼──┼──┼──┨
┃~~│~~│~~│~~│~~│XX│XX│XX│  │  ┃
┗━━┷━━┷━━┷━━┷━━┷━━┷━━┷━━┷━━┷━━┛


C:\Code\GitHub\NBattleshipCodingContest\NBattleshipCodingContest>

Interactive Games

You can run games interactively. For that, run the program with the command line parameters manager -b. Once you started it, open a web browser and navigate to https://localhost:5001/.

Interactive game result

Clone this wiki locally