Code:
/* This program has been designed to implement the game Gomoku (also know as five in a row). A user plays the game on a
* 15x15 board which is empty initially. The users will take turns to place one game piece (x for computer, O for the user)
* on the board.
*
* The player who first creates "five in a row", either horziontally, vertically, or diagonally, wins the game.
*
* This program will have the following features:
* (1) interact with the user playing the game,
* (2) contain two game strategies for computer
* (3) a design to show who is the winner.
*
* Terminal line command: a.out k moves boards
* Where k is option 0 for random computer moves and 1 is smart computer moves against the user*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 17
#define COLUMNS 19
void message(char *argv[])
{
printf("Usage:\n"
"%s [arg] [arg] [arg]\n"
"0 for random\n"
"1 for smart strategy\n"
"Filename for output\n"
"Filename for gameboard status\n", argv[0]);
}
int main ( int argc, char *argv[]){
FILE *moves;
FILE *boards;
if ( argc < 2)
{
message(argv);
return -1;
}
int k; //this will be part of the command for the user to choose 0 for a random strategy against the computer or 1 for smart strategy
k = atoi(argv[1]);
if ( ( moves = fopen(argv[2],"w")) == NULL) // this is the output file name that records te moves made by two players (the CPU and user)
{
printf("Cannot open file %s\n", argv[2]);
return -1;
}
if ( ( boards = fopen(argv[3],"w")) == NULL )// will record the game board status after each move
{
printf("Cannot open file %s\n", argv[3]);
return -1;
}
int i,j;
char x_mark = 'X'; // the player's marker value
char o_mark = 'O'; // the cpu's marker
char x_coord; //the moves the character will input onto the board
char y_coord; // the move the player will input onto the board;
int flag;
int game;
char board[ROWS][COLUMNS]={
{'o','|'},
{'n','|'},
{'m','|'},
{'l','|'},
{'k','|'},
{'j','|'},
{'i','|'},
{'h','|'},
{'g','|'},
{'f','|'},
{'e','|'},
{'d','|'},
{'c','|'},
{'b','|'},
{'a','|'},
{' ',' ','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-' },
{' ',' ','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'},
};
for (i = 0; i<ROWS;i++){
for(j = 0; j<COLUMNS;j++){
board[i][j];
// printf("%c",board[i][j]);
}
}
for (i=0;i<ROWS;i++) //Printing a blank board
{
for(j=0;j<COLUMNS;j++)
{
printf("%c ",board[i][j]);
}
printf("\n");
}
int i_coord, j_coord;
while (k == 0) //This option is basically where the user faces against the CPU. the CPU has randomly generated moves
{
// you're taking in char ( as int for both)
printf("Please choose a move\n"
"x :");
x_coord = getchar(); // grabs x
getchar(); // stops loop to get next
printf("please enter y move: ");
y_coord = getchar(); // grabs y
/*
scanf("%c",&x_coord); //,&y_coord);
scanf("%c",&y_coord);
*/
printf(" Player 1 Moves: x %c %c \n",x_coord, y_coord);
fprintf(moves, " Player 1 Moves: %c%c \n",x_coord, y_coord);
printf(" User move: %c %c \n",x_coord,y_coord);
switch(x_coord){
case '1': j_coord = 2; printf("case 1 %d\n",j_coord ); break;
case '2': j_coord = 3; break;
case '3': j_coord = 4; break;
case '4': j_coord = 5; break;
case '5': j_coord = 6; break;
case '6': j_coord = 7; break;
case '7': j_coord = 8; break;
case '8': j_coord = 9; break;
case '9': j_coord = 10; break;
case 'A': j_coord = 11; break;
case 'B': j_coord = 12; break;
case 'C': j_coord = 13; break;
case 'D': j_coord = 14; break;
case 'E': j_coord = 15; break;
case 'F': j_coord = 16; break;
}
switch(y_coord){
case 'a': i_coord = 14; printf("case a %d\n", i_coord); break;
case 'b': i_coord = 13; break;
case 'c': i_coord = 12; break;
case 'd': i_coord = 11; break;
case 'e': i_coord = 10; break;
case 'f': i_coord = 9; break;
case 'g': i_coord = 8; break;
case 'h': i_coord = 7; break;
case 'i': i_coord = 6; break;
case 'j': i_coord = 5; break;
case 'k': i_coord = 4; break;
case 'l': i_coord = 3; break;
case 'm': i_coord = 2; break;
case 'n': i_coord = 1; break;
case 'o': i_coord = 0; break;
}
printf("x = %d, y = %d \n",j_coord,i_coord);
printf("here\n");
board[i_coord][j_coord]= x_mark;
for (i=0;i<ROWS;i++)
{
for(j=0;j<COLUMNS;j++)
{
printf("%c ",board[i][j]);
}
printf("\n");
}
}
if (x_coord >'F' || y_coord >'o' ){
printf("The Users input is invalid. Please Try again! ");
}
return 0;
}
results