|
| 1 | +#define _CRT_SECURE_NO_WARNINGS |
1 | 2 | #include <stdio.h> |
2 | 3 | #include <stdlib.h> |
3 | 4 | #include <stdbool.h> |
|
6 | 7 | #include "windowsConsoleInteraction.h" |
7 | 8 | #include <conio.h> |
8 | 9 |
|
| 10 | +typedef struct Coordinates { |
| 11 | + short i, j; |
| 12 | +}coord; |
| 13 | + |
| 14 | +coord clickToCoordinates(COORD click) |
| 15 | +{ |
| 16 | + coord retValue; |
| 17 | + |
| 18 | + for (int count = 0; count < 37; count += 9) |
| 19 | + { |
| 20 | + if (click.Y > count && click.Y < count + 9) |
| 21 | + retValue.i = count/9; |
| 22 | + } |
| 23 | + for (int count = 0; count < 109; count += 4) |
| 24 | + { |
| 25 | + if (click.X > count && click.X < count + 4) |
| 26 | + retValue.j = count/4; |
| 27 | + } |
| 28 | + return retValue; |
| 29 | +} |
| 30 | + |
| 31 | +void createDisplayeGrid(char displayGrid[37][109]) |
| 32 | +{ |
| 33 | + for (int i = 0; i < 37; i++) |
| 34 | + { |
| 35 | + for (int j = 0; j < 109; j++) |
| 36 | + { |
| 37 | + if (j % 9 == 0 || i % 4 == 0) |
| 38 | + { |
| 39 | + if (i % 4 == 0) |
| 40 | + displayGrid[i][j] = ':'; |
| 41 | + |
| 42 | + if (j % 9 == 0) |
| 43 | + displayGrid[i][j] = '|'; |
| 44 | + } |
| 45 | + else |
| 46 | + displayGrid[i][j] = ' '; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void modifyDisplayGrid(char displayGrid[37][109], coord coordinates, char symbol) |
| 52 | +{ |
| 53 | + char horizontalBow[3][8] = { {' ', ' ', ' ', ' ', ' ', ' ', ' ', '_'} , |
| 54 | + {' ', ' ', ' ', ' ', '_', '_', '/', ' '} , |
| 55 | + {' ', ' ', ' ', ' ', '\\', '_', '_', '_'} }; |
| 56 | + |
| 57 | + char horizontalMiddle[3][8] = { {'_', '_', '_', '_', '_', '_', '_', '_'} , |
| 58 | + {' ', ' ', '|', '_', 'o', '_', '|', ' '} , |
| 59 | + {'_', '_', '_', '_', '_', '_', '_', '_'} }; |
| 60 | + |
| 61 | + char horizontalStern[3][8] = { {'_', '_', '_', ' ', ' ', ' ', ' ', ' '} , |
| 62 | + {' ', ' ', ' ', '|', ' ', ' ', ' ', ' '} , |
| 63 | + {'_', '_', '/', ' ', ' ', ' ', ' ', ' '} }; |
| 64 | + |
| 65 | + char verticalBow[3][8] = { {' ', ' ', ' ', '/', '\\', ' ', ' ', ' '} , |
| 66 | + {' ', ' ', '/', '_', '_', '\\', ' ', ' '} , |
| 67 | + {' ', '|', ' ', ' ', ' ', ' ', '|', ' '} }; |
| 68 | + |
| 69 | + char verticalMiddle[3][8] = { {' ', '|', ' ', ' ', ' ', ' ', '|', ' '} , |
| 70 | + {' ', '|', ' ', ' ', ' ', ' ', '|', ' '} , |
| 71 | + {' ', '|', ' ', ' ', ' ', ' ', '|', ' '} }; |
| 72 | + |
| 73 | + char verticalStern[3][8] = { {' ', '|', ' ', '_', '_', ' ', '|', ' '} , |
| 74 | + {' ', '|', '_', '_', '_', '_', '|', ' '} , |
| 75 | + {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '} }; |
| 76 | + |
| 77 | + char nope[3][8] = { {' ', ' ', '\\', '_', '_', ' / ', ' ', ' '} , |
| 78 | + {' ', '-', '|', '_', '_', '|', '-', ' '} , |
| 79 | + {' ', ' ', '/', ' ', ' ', '\\', ' ', ' '} }; |
| 80 | + |
| 81 | + char submarine[3][18] = { {' ', ' ', ' ', ' ', '_', ' ', ' ', ' '} , |
| 82 | + {' ', '_', '_', '/', 'o', '\\', '_', ' '} , |
| 83 | + {'|', '_', '_', '_', '_', '_', '_', '|'} }; |
| 84 | + |
| 85 | + coord start; |
| 86 | + |
| 87 | + for (int count = 0; count < 9; count++) |
| 88 | + { |
| 89 | + if (coordinates.i == count) |
| 90 | + start.i = (count * 4) + 1; |
| 91 | + } |
| 92 | + for (int count = 0; count < 11; count++) |
| 93 | + { |
| 94 | + if (coordinates.j == count) |
| 95 | + start.j = (count * 9) + 1; |
| 96 | + } |
| 97 | + |
| 98 | + for (int i = 0; i < 3; i++) |
| 99 | + { |
| 100 | + for (int j = 0; j < 18; j++) |
| 101 | + { |
| 102 | + if (symbol == '<') |
| 103 | + displayGrid[start.i + i][start.j + j] = horizontalBow[i][j]; |
| 104 | + if (symbol == '=') |
| 105 | + displayGrid[start.i + i][start.j + j] = horizontalMiddle[i][j]; |
| 106 | + if (symbol == '>') |
| 107 | + displayGrid[start.i + i][start.j + j] = horizontalStern[i][j]; |
| 108 | + if (symbol == '^') |
| 109 | + displayGrid[start.i + i][start.j + j] = verticalBow[i][j]; |
| 110 | + if (symbol == '|') |
| 111 | + displayGrid[start.i + i][start.j + j] = verticalMiddle[i][j]; |
| 112 | + if (symbol == '~') |
| 113 | + displayGrid[start.i + i][start.j + j] = verticalStern[i][j]; |
| 114 | + if (symbol == ' ') |
| 115 | + displayGrid[start.i + i][start.j + j] = nope[i][j]; |
| 116 | + if (symbol == 'o') |
| 117 | + displayGrid[start.i + i][start.j + j] = submarine[i][j]; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +void printDisplayGrid(char displayGrid[37][109]) |
| 124 | +{ |
| 125 | + for (int i = 0; i < 37; i++) |
| 126 | + { |
| 127 | + moveRight(1); |
| 128 | + for (int j = 0; j < 109; j++) |
| 129 | + printf("%c", displayGrid[i][j]); |
| 130 | + printf("\n"); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +bool isEnd(char grid[9][11], char playerGrid[9][11]) |
| 135 | +{ |
| 136 | + bool isEnd = true; |
| 137 | + for (int i = 0; i < 9; i++) |
| 138 | + { |
| 139 | + for (int j = 0; j < 11; j++) |
| 140 | + if (grid[i][j] != playerGrid[i][j]) |
| 141 | + isEnd = false; |
| 142 | + } |
| 143 | + return isEnd; |
| 144 | +} |
| 145 | + |
9 | 146 | int main(int argc, char** argv) |
10 | 147 | { |
| 148 | + if (argc < 2) |
| 149 | + { |
| 150 | + printf("\tusage: battleship <file>\n"); |
| 151 | + return -1; |
| 152 | + } |
| 153 | + |
| 154 | + FILE* textFile; |
| 155 | + textFile = fopen(argv[1], "r"); |
| 156 | + if (textFile == NULL) |
| 157 | + { |
| 158 | + puts("Couldn't open file"); |
| 159 | + return -2; |
| 160 | + } |
| 161 | + |
| 162 | + char displayGrid[37][109]; |
| 163 | + char grid[9][11]; |
| 164 | + char playGrid[9][11]; |
| 165 | + coord play; |
| 166 | + EVENT retEvent; |
| 167 | + COORD mouseCoord; |
| 168 | + char trashcan[10]; |
| 169 | + |
| 170 | + { |
| 171 | + char getGrid[9][25] = { 0 }; |
| 172 | + bool pass = true; |
| 173 | + for (int count = 0; count < 9; count++) |
| 174 | + fgets(getGrid[count], 25, textFile); |
| 175 | + |
| 176 | + for (int count = 0; count < 9; count++) |
| 177 | + { |
| 178 | + if (getGrid[count][24] != '\0' && getGrid[count][24] != '\n') |
| 179 | + { |
| 180 | + pass = false; |
| 181 | + break; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if (pass == false) |
| 186 | + { |
| 187 | + puts("the file isn't formatted correctly, please try again"); |
| 188 | + return -1; |
| 189 | + } |
| 190 | + |
| 191 | + for (int i = 0; i < 9; i++) |
| 192 | + { |
| 193 | + for (int j = 1; j < 22; j += 2) |
| 194 | + grid[i][j - (j/2 + 1)] = getGrid[i][j]; |
| 195 | + } |
| 196 | + |
| 197 | + for (int i = 0; i < 9; i++) |
| 198 | + { |
| 199 | + for (int j = 0; j < 11; j++) |
| 200 | + playGrid[i][j] = ' '; |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + { |
| 206 | + int windowSize[2]; |
| 207 | + setupConsole(); |
| 208 | + moveTo(999, 999); |
| 209 | + getCursorPosition(&windowSize[0], &windowSize[1]); |
| 210 | + moveTo(0, 0); |
| 211 | + |
| 212 | + while (windowSize[0] < 40) |
| 213 | + { |
| 214 | + puts("Please, expand the console window!"); |
| 215 | + moveTo(999, 999); |
| 216 | + getCursorPosition(&windowSize[0], &windowSize[1]); |
| 217 | + moveTo(0, 0); |
| 218 | + } |
| 219 | + clearScreenToBottom(); |
| 220 | + restoreConsoleMode(); |
| 221 | + } |
| 222 | + |
| 223 | + createDisplayeGrid(displayGrid); |
| 224 | + printDisplayGrid(displayGrid); |
| 225 | + for (bool playing = true; playing == true;) |
| 226 | + { |
| 227 | + while (true) |
| 228 | + { |
| 229 | + retEvent.event.mouseEvent = 0xc00; |
| 230 | + retEvent = eventMain(); |
| 231 | + mouseCoord = retEvent.event.mouseCoord; |
| 232 | + if (retEvent.event.mouseEvent == FROM_LEFT_1ST_BUTTON_PRESSED) |
| 233 | + break; |
| 234 | + } |
| 235 | + printf("%i %i\n", mouseCoord.Y, mouseCoord.X); |
| 236 | + play = clickToCoordinates(mouseCoord); |
| 237 | + printf("%i %i\n", play.i, play.j); |
| 238 | + fgets(trashcan, 5, stdin); |
| 239 | + |
| 240 | + modifyDisplayGrid(displayGrid, play, grid[play.i][play.j]); |
| 241 | + clearScreenToTop; |
| 242 | + moveTo(0, 0); |
| 243 | + printDisplayGrid(displayGrid); |
| 244 | + |
| 245 | + if (isEnd(grid, playGrid)) |
| 246 | + { |
| 247 | + puts("Game Over!"); |
| 248 | + fgets(trashcan, 5, stdin); |
| 249 | + break; |
| 250 | + } |
| 251 | + } |
11 | 252 |
|
12 | 253 |
|
13 | 254 | return 0; |
|
0 commit comments