A naïve way to find the winner at the ‘Five in a row’ board game – C Source Code

A

Five in a row is a relatively easy game for two players. The aim is to get five of one’s pieces in a row (horizontal, vertical, or diagonal). Below is a simple algorithm in C to find the winner.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

// Example tables for testing

uint8_t table_t1[8][8] = 
{ 	
   {0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
   {0,0,0,0,0,0,0,0},{0,0,0,0,2,0,0,0},
   {0,0,0,2,0,0,0,0},{0,0,2,0,0,0,0,0},
   {0,2,0,0,0,0,0,0},{2,0,0,0,0,0,0,0}
};

uint8_t table_t2[8][8] =
{
   {0,0,0,1,1,1,1,1},{0,0,0,0,0,0,0,0},
   {0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
   {0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
   {0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}
};

uint8_t table_t3[8][8] =
{
   {0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
   {0,0,0,0,2,0,0,0},{0,0,0,0,2,0,0,0},
   {0,0,0,0,2,0,0,0},{0,0,0,0,2,0,0,0},
   {0,0,0,0,2,0,0,0},{0,0,0,0,0,0,0,0}
};

uint8_t table_t4[8][8] =
{
   {0,0,0,0,0,0,0,0},{0,1,0,0,0,0,0,0},
   {0,0,1,0,2,0,0,0},{0,0,0,1,2,0,0,0},
   {0,0,0,0,1,0,0,0},{0,0,0,0,2,1,0,0},
   {0,0,0,0,2,0,0,0},{0,0,0,0,0,0,0,0}
};

uint8_t check_player_status_horizontal(uint8_t table[8][8], int i, int j)
{
   for (int t = i; t < i + 5; t++)
      if (table[t][j] != table[i][j])
         return 0;
   return 1;
}
uint8_t check_player_status_vertical(uint8_t table[8][8], int i, int j)
{
   for (int t = j; t < j + 5; t++)
      if (table[i][t] != table[i][j])
         return 0;
   return 1;
}
uint8_t check_player_status_diag_forwards(uint8_t table[8][8], int i, int j)
{
   for (int t = 0; t < 5; t++)
      if (table[i+t][j+t] != table[i][j])
         return 0;
   return 1;
}
uint8_t check_player_status_diag_backwards(uint8_t table[8][8], int i, int j)
{
   for (int t = 0; t < 5; t++)
      if (table[i - t][j + t] != table[i][j])
         return 0;
   return 1;
}

uint8_t check_player_status(uint8_t table[8][8], uint8_t p)
{
   uint8_t result = 0;

   for(int i=0;i<8;i++)
      for (int j = 0; j < 8; j++)
      { 
         if (table[i][j] == p)
	 {
	    if (i <= 3)
	      result |= check_player_status_horizontal(table, i, j);
	    if (j <= 3)
	      result |= check_player_status_vertical(table, i, j);
	    if (i<=3 && j<=3)
	      result |= check_player_status_diag_forwards(table, i, j);
	    if (i > 3 && j <= 3)
	      result |= check_player_status_diag_backwards(table, i, j);
	 }
	 if (result == 1)
	    return 1;
      }
   return result;
}


int main()
{
   uint8_t p1 = 1, p2 = 2,r1,r2;

   r1 = check_player_status(table_t1, 1);
   r2 = check_player_status(table_t1, 2);
   printf("%d - %d\n", r1, r2);

   r1 = check_player_status(table_t2, 1);
   r2 = check_player_status(table_t2, 2);
   printf("%d - %d\n", r1, r2);

   r1 = check_player_status(table_t3, 1);
   r2 = check_player_status(table_t3, 2);
   printf("%d - %d\n", r1, r2);

   r1 = check_player_status(table_t4, 1);
   r2 = check_player_status(table_t4, 2);
   printf("%d - %d\n", r1, r2);
   
   return 0;
}

1 comment

Categories

Tags

Subscribe to our Newsletter