Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Version 6 with ai
- #include "stdio.h"
- #include "windows.h"
- #include <time.h>
- const int n = 110;
- const int s = 115;
- const int e = 101;
- const int w = 119;
- void DrawBoard(char* grid)
- {
- int count = 0;
- printf(" 0 1 2 3 4 5 6 7 8 9\n");
- for(int a = 0; a < 10; ++a)
- {
- printf("%d",a);
- for(int b = 0; b < 10; b++)
- {
- printf("%c ",grid[count]);
- ++count;
- }
- printf("\n");
- }
- }
- void PlaceShips(char* grid, int& shipSize)
- {
- int x = 0;
- int y = 0;
- char direction = 0;
- int error = 0;
- int error2 = 0;
- printf("Place Ship %d wide\n", shipSize);
- while(error != 1 || error2 != 1)
- {
- printf("X:");
- error = scanf_s("%d",&x);
- printf("Y:");
- error2 = scanf_s("%d",&y);
- fflush(stdin);
- if(x < 0 || x > 9 || y < 0 || y > 9 || grid[y*10+x]!='=')
- {
- printf("Try again. Invalid number entered\n");
- error = 99;
- }
- }
- error = 0;
- while(error != 1)
- {
- printf("What Direction should it be placed in?\n");
- error = scanf_s("%c",&direction);
- fflush(stdin);
- if(direction == 'n' || direction == 'e' || direction == 'w' || direction == 's' || error != 1)
- {
- int position = (((y + 1) * 10 + x) + 1) - 11;
- switch(direction)
- {
- case n:
- {
- if((y-shipSize+1)<0)
- {
- printf("Not enough room\n");
- error = 0;
- }
- else if(grid[position - shipSize * 10 + 10] == '=')
- {
- for(int a = 0; a < shipSize; ++a)
- {
- grid[position - a * 10] = 's';
- }
- --shipSize;
- }
- else
- {
- printf("Not enough room\n");
- error = 0;
- }
- break;
- }
- case e:
- {
- if((x+shipSize-1)>9)
- {
- printf("Not enough room\n");
- error = 0;
- }
- else if(grid[position + shipSize-1] == '=')
- {
- for(int a = 0; a < shipSize; ++a)
- {
- grid[position + a] = 's';
- }
- --shipSize;
- }
- else
- {
- printf("Not enough room\n");
- error = 0;
- }
- break;
- }
- case w:
- {
- if((x-shipSize+1)<0)
- {
- printf("Not enough room\n");
- error = 0;
- }
- else if(grid[position - shipSize] == '=')
- {
- for(int a = 0; a < shipSize; ++a)
- {
- grid[position - a] = 's';
- }
- --shipSize;
- }
- else
- {
- printf("Not enough room\n");
- error = 0;
- }
- break;
- }
- case s:
- {
- if((y+shipSize-1)>9)
- {
- printf("Not enough room\n");
- error = 0;
- }
- else if(grid[position + shipSize * 10 - 10] == '=')
- {
- for(int a = 0; a < shipSize; ++a)
- {
- grid[position + a * 10] = 's';
- }
- --shipSize;
- }
- else
- {
- printf("Not enough room\n");
- error = 0;
- }
- break;
- }
- }
- }
- else
- {
- printf("Error, try again\n");
- fflush(stdin);
- error = 0;
- }
- }
- }
- void PlayerAttack(char* opponent, char* record, int &count)
- {
- int x = 0;
- int y = 0;
- int error = 0;
- int error2 = 0;
- printf("Which spot would you like to attack?\n");
- while(error != 1 || error2 != 1)
- {
- printf("X:");
- error = scanf_s("%d",&x);
- printf("Y:");
- error2 = scanf_s("%d",&y);
- fflush(stdin);
- if(x < 0 || x > 9 || y < 0 || y > 9)
- {
- printf("Try again. Invalid number entered\n");
- error = 99;
- }
- else if(record[y*10+x]!='=')
- {
- printf("Try again. Already tried the spot\n");
- error = 99;
- }
- }
- if(opponent[y*10+x]!='s')
- {
- record[y*10+x] = 'm';
- printf("It's a miss!\n");
- }
- else
- {
- record[y*10+x] = 'h';
- printf("It's a hit!\n");
- --count;
- }
- }
- //=================================ai=============================
- int AIFire(int lastHit, int lasterHit, char grid[100])
- {
- int target = -1;
- int targetDir = -1;
- bool fired = false;
- while (!fired)
- {
- if ((lastHit == -1)&&(lasterHit == -1))
- {
- target = rand() % 100;
- if (grid[target] != 'm')
- {
- fired = true;
- }
- }
- else
- {
- if (lasterHit == -1)
- {
- targetDir = rand() % 4 + 1;
- switch (targetDir)
- {
- case 1: //UP
- {
- if (((lastHit - 10) > 0)&&(grid[lastHit - 10] != 'X')&&(grid[lastHit - 10] != 'm'))
- {
- target = (lastHit - 10);
- fired = true;
- }
- }
- case 2: //DOWN
- {
- if (((lastHit + 10) < 100)&&(grid[lastHit + 10] != 'X')&&(grid[lastHit + 10] != 'm'))
- {
- target = (lastHit + 10);
- fired = true;
- }
- }
- case 3: //LEFT
- {
- if (((lastHit - 1) > 0)&&(grid[lastHit - 1] != 'X')&&((lastHit % 10) != 0)&&(grid[lastHit - 1] != 'm'))
- {
- target = (lastHit - 1);
- fired = true;
- }
- }
- case 4: //RIGHT
- {
- if (((lastHit + 1) < 100)&&(grid[lastHit + 1] != 'X')&&(((lastHit + 1) % 10) != 0)&&(grid[lastHit + 1] != 'm'))
- {
- target = (lastHit + 1);
- fired = true;
- }
- }
- }
- }
- else
- {
- if (((lastHit - 1) == lasterHit)||((lastHit + 1) == lasterHit))
- {
- if ((lastHit > lasterHit)&&(grid[lastHit + 1] != 'X'))
- {
- if (grid[lastHit + 1] != 'm')
- {
- target = lastHit + 1;
- fired = true;
- }
- else
- {
- int i = lastHit;
- while (grid[i] != '=')
- {
- i--;
- }
- target = i;
- fired = true;
- }
- }
- if ((lastHit < lasterHit)&&(grid[lastHit - 1] != 'X'))
- {
- target = lastHit - 1;
- fired = true;
- }
- }
- if (((lastHit - 10) == lasterHit)||((lastHit + 10) == lasterHit))
- {
- if ((lastHit > lasterHit)&&(grid[lastHit + 10] != 'X'))
- {
- target = lastHit + 10;
- fired = true;
- }
- if ((lastHit < lasterHit)&&(grid[lastHit - 10] != 'X'))
- {
- target = lastHit - 10;
- fired = true;
- }
- }
- }
- }
- }
- return target;
- }
- //---------------------------------------------------------------
- void main()
- {
- //ai stuff
- srand(time(NULL));
- int lastHit = -1;//needed in main
- int lasterHit = -1;
- //first two for player, other two for computer
- char grid[100];
- char grid1[100];
- char grid2[100];
- //char grid3[100];
- //count the ship left for player and computer for winning condition checking
- int playerShipNum = 17;
- int comShipNum = 17;
- // Clear Grid
- for(int a = 0; a < 100; ++a)
- {
- grid[a] = '=';
- grid1[a] = '=';
- grid2[a] = '=';
- //grid3[a] = '=';
- }
- int shipSize = 5;
- DrawBoard(grid);// first draw
- // =======================================SHIP PLACMENT====================================================================
- bool is3ship = false;
- for(int a = 0; a < 5; ++a) // once this is done all of the players ships are placed----------------------------------------
- {
- PlaceShips(grid, shipSize);
- system("cls");
- DrawBoard(grid);
- if(shipSize == 2) // pretty function to get double 3 ships
- {
- if(is3ship == false)
- {
- ++shipSize;
- is3ship = true;
- }
- }
- } // -----------------------------------------------------------------------------------------------------------------------
- system("pause");
- system("cls");
- //Start the game
- while(comShipNum!=0&&playerShipNum!=0)
- {
- //These are for testing functions purpose
- /*DrawBoard(grid);
- DrawBoard(grid1);
- PlayerAttack(grid,grid1,comShipNum);*/
- //player's turn
- printf("Player's turn:\n");
- DrawBoard(grid1);
- PlayerAttack(grid2,grid1,comShipNum);
- system("pause");
- system("CLS");
- printf("Computer's turn:\n");
- //computer attack function goes here... I think is the AI function?
- int target = AIFire(lastHit, lasterHit, grid);// needs to know when a ship has been sunk
- // check if the loction is a hit players grid
- //if hit need to reduce the playerShipNum
- DrawBoard(grid);
- system("pause");
- system("CLS");
- }
- if(comShipNum==0)
- {
- printf("You win!!\n");
- }
- else
- {
- printf("You loss...\n");
- }
- system("pause");
- }
Add Comment
Please, Sign In to add comment