Click here to Skip to main content
15,558,557 members
Articles / Programming Languages / C#
Article
Posted 12 Nov 2003

Stats

94.9K views
769 downloads
21 bookmarked

Link 4 game with intermediate computer intelligence

Rate me:
Please Sign up or sign in to vote.
3.40/5 (5 votes)
17 Nov 20032 min read
Lets you play a game of link 4 against the computer

Image 1

Introduction

Just starting to learn programming in C#, I thought it would be nice to start out with a simple program that lets you play a nice game of link 4 against the program. Nothing too fancy though as Windows applications are not my thing (yet). It's a console application with medium-low computer intelligence.

Some of the code comments and names are still in Dutch (sorry ;-) ) but that is being worked on.

The goal of the program is to eventually get a fully running game of link4 with an unbeatable computer. The best one should be able to get is a draw, but there's still some work to be done.

The goal of the game is to get 4 chips or tokens (what you want to call them) in a row (either diagonally, horizontally or vertically) and be a winner. :)

Using the code

The program consists of four classes:

Class MainFunctions...

...which contains the methods:

  • Main() --> ...the entry point
  • PrintBord(Gameboard fld) --> prints the current state of the game
  • initGameboard(Gameboard fld) --> sets all values in the 2D array to "."
  • askMove(Gameboard fld) --> Asks the human player to enter the column number in which to drop the token
  • doMove(Gameboard fld, int col, string player) --> places the move selected by either human or computer
  • askNewGame() --> Asks the human player if he || she wants to play another game

Here's the Main() method which controls the main functions of the program with some while-loops, two of which have no body as there is no reason for them to have one.

C#
static void Main()
{
  do
  {
    // Create gameboard
    Gameboard gbField = new Gameboard();
    Gameboard gbDontMove = new Gameboard();
      
    // Set all fields in array to ".", prepare for new game
    initGameboard(gbField);
        
    do
    {
      PrintBord(gbField);

      while(!doMove(gbField, askMove(gbField), "M")) 
      {
        // Do nothing in here: All methods are in the while() statement
        // Just keeps looping until something valid is returned...
      }
  
      if(EndOfGame.endOfGame(gbField)) break;
        
      while(!doMove(gbField, CompuIntel.getComputerMove(
          gbField, gbDontMove), "C")) 
      {
        // Do nothing in here: All methods are in the while() statement
        // Just keeps looping until something valid is returned...
      }

      if(EndOfGame.endOfGame(gbField)) break;
  
    } while (!EndOfGame.endOfGame(gbField));

  }while( askNewGame() );
}

Class Gameboard...

...contains constructors used for setting a game field.

C#
public class Gameboard
{
  private static int maxrow = 6;
  private static int maxcol = 7;

  public string[] asPlayers = {"M", "C"}; 
  public string[,] position = new string[maxrow, maxcol];

  public int iMAXROW
  {
    get { return maxrow; }
  }

  public int iMAXCOL
  {
    get { return maxcol; }
  }
}

Class EndOfGame...

...which holds functions that check for an end of game through a win or draw.

  • endOfGame(Gameboard fld) --> Controls the endOfGame check
  • draw(Gameboard fld) --> iterates through all members of the array checking for empty positions. If no "." is found the game has ended.
  • CheckWinst(Gameboard fld, string player) --> Checks for four tokens in a row for both players horizontal, vertical and diagonal.

Class CompuIntel...

...holds the logic of the moves made by the computer. It's not quite complete yet as only direct wins are recognized. The only method in this class is getComputerMove(Gameboard fld, Gameboard dm). The Gamefield dm doesn't do anything at this time, but it's going to be used as a field in which the moves NOT to make will be held. You'll see why this is necessary when you play the game :)

Points of interest

At this point the program isn't that intelligent. It checks for direct winning chances for both computer and human player (in that order) and only makes a well thought through move when there's a direct chance of winning for either player. In all other cases it places a random move.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
Dennis is currently working as a telecommunications-system administrator for MyTravel Nederland, but has always had a big interest in programming ever since he got his own MSX for his birthday.

For some time programming has stayed in the background (not counting some VB programming Wink | ;-) ), but he recently rediscovered it when enterig the magical world of c#.



Interest and hobbies can be found at his homepage (which is still under construction).

Comments and Discussions

 
GeneralMuch better AI needed for your goal Pin
3ddA17-Nov-03 23:17
3ddA17-Nov-03 23:17 
GeneralRe: Much better AI needed for your goal Pin
scadaguy19-Nov-03 7:34
scadaguy19-Nov-03 7:34 
GeneralSo... Pin
Uwe Keim13-Nov-03 4:27
sitebuilderUwe Keim13-Nov-03 4:27 
GeneralRe: So... Pin
Dennis van Niel13-Nov-03 4:35
Dennis van Niel13-Nov-03 4:35 
GeneralAdd a diagram then Pin
dog_spawn13-Nov-03 6:01
dog_spawn13-Nov-03 6:01 
GeneralRe: Add a diagram then Pin
Dennis van Niel13-Nov-03 6:20
Dennis van Niel13-Nov-03 6:20 
GeneralRe: Add a diagram then Pin
dog_spawn13-Nov-03 6:27
dog_spawn13-Nov-03 6:27 
GeneralRe: Add a diagram then Pin
Marc Clifton13-Nov-03 16:44
mvaMarc Clifton13-Nov-03 16:44 
GeneralRe: Add a diagram then Pin
dog_spawn14-Nov-03 8:05
dog_spawn14-Nov-03 8:05 
GeneralRe: Add a diagram then Pin
Ryan Beesley15-Nov-03 2:21
Ryan Beesley15-Nov-03 2:21 
dog_spawn wrote:
Look at Halo on the PC, it tooks years to change graphics platform.

If you are trying to suggest that it took "years" to port Halo from the XBox to the PC because it is coded in C, you are gravely mistaken.

The decision to delay a port of the game to the PC was entirely marketing. Halo was/is one of the best selling games for the XBox platform or any platform for that matter. It was a first generation game and was relied upon to drive console sales. If that game were ported to the PC too soon after its XBox debut, it would cut into those sales.
GeneralRe: Add a diagram then Pin
dog_spawn15-Nov-03 4:04
dog_spawn15-Nov-03 4:04 
GeneralRe: Add a diagram then Pin
converdb5-Dec-03 12:52
converdb5-Dec-03 12:52 
GeneralRe: Add a diagram then Pin
dog_spawn6-Dec-03 6:06
dog_spawn6-Dec-03 6:06 
GeneralRe: So... Pin
converdb5-Dec-03 12:48
converdb5-Dec-03 12:48 
GeneralRe: So... Pin
Thomas Freudenberg17-Nov-03 21:47
Thomas Freudenberg17-Nov-03 21:47 
GeneralRe: So... Pin
Anonymous26-Nov-03 16:22
Anonymous26-Nov-03 16:22 
GeneralRe: So... Pin
dog_spawn6-Dec-03 6:08
dog_spawn6-Dec-03 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.