I started out to learn more about C to learn more about low level programming and hardware and I can improve.
It generates a random number between 1 and 4 then stores it into two variables.
It takes in to x,y board values ranging to 1,4 in the board then if the two inputs equal the random values it gives a point and resets but if not it marks the board and takes one try away. If the amount of tries reaches 5 then it resets the board
It's basic and meant to only have one target for each round and I did not do any error checking for ascii inputs.
/*---------#include---------*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
/*---------value definitions---------*/
#define BOARD_SIZE 5
#define LOW_BOUNDS 0
#define HIGH_BOUNDS 4
#define TRIES 5
//functions
void DRAW_BOARD();
int GEN_RND_ZERO();
void CLEAR_BOARD();
int I1,I2; //Iterator values
char LAYOUT[BOARD_SIZE][BOARD_SIZE] =
{
{'X','1','2','3','4'},
{'1','o','o','o','o'},
{'2','o','o','o','o'},
{'3','o','o','o','o'},
{'4','o','o','o','o'},
};
/*-----MAIN-----*/
int main()
{
srand(time(NULL)); //rnd val seed
unsigned short INPUT1,INPUT2;
int RND1,RND2;
unsigned short PNT = 0,Counter;
printf("-Battleship Game-\n");
sleep(2);
set1:
RND1 = GEN_RND_ZERO();
RND2 = GEN_RND_ZERO();
set2:
printf("\n%d %d\n\n",RND1,RND2);
DRAW_BOARD();
printf("\nEnter an X value: "); scanf("%hu",&INPUT1);
printf("\nEnter a Y value: "); scanf("%hu",&INPUT2);
//Input error check
if(INPUT1 <= 0 || INPUT1 > 4 || INPUT2 <= 0 || INPUT2 > 4)
{
printf("\n\nInput failure\n\n");
sleep(3);
system("cls");
goto set2;
}
if(INPUT1==RND1&&INPUT2==RND2)
{
PNT++;
printf("\n\nPoint increase [%d]\n",PNT);
CLEAR_BOARD();
sleep(3); system("cls"); goto set1;
}
else
{
Counter++;
printf("\n\nIncorrect\n\n");
LAYOUT[INPUT1][INPUT2] = 'x';
if(Counter == TRIES)
{
printf("Out of tries");
Counter = 0;
sleep(3); system("cls"); goto set1;
}
sleep(3); system("cls"); goto set2;
}
}
/*---------function bodys---------*/
//Draws the player board
void DRAW_BOARD()
{
for(I1 = 0; I1<BOARD_SIZE; I1++)
{
for(I2 = 0; I2<BOARD_SIZE; I2++)
{
printf("%c ",LAYOUT[I1][I2]);
}
printf("\n");
}
}
//Replace all marked characters back to an o
void CLEAR_BOARD()
{
for(I1 = 1; I1<BOARD_SIZE; I1++)
{
for(I2 = 1; I2<BOARD_SIZE; I2++)
{
LAYOUT[I1][I2] = 'o';
}
printf("\n");
}
}
//Checks if the random value generated is zero if it is then it will increment to 1
int GEN_RND_ZERO()
{
int temp = rand()%4;
//Pure Laziness
if(temp == 0) {
temp++;
}
return temp;
}