1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
#include <windows.h>
#include <stdio.h>
using namespace std;
constexpr char empty = '.';
constexpr int GridSize = 26;
char grid[GridSize][GridSize];
//Random number generating function
int random(int from, int to)
{
return rand() % (to - from + 1) + from;
}
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE) return;
/*Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/*Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter( hStdOut,
(TCHAR)
' ',
cellCount,
homeCoords, &count
)) return;
/*Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute( hStdOut,
csbi.wAttributes,
cellCount,
homeCoords, &count
)) return;
/*Move the cursor home */
SetConsoleCursorPosition(hStdOut, homeCoords);
}
//function to calculate the distance between characters
double distanceBetweenTwoPoints(double x, double y, double a, double b)
{
return sqrt(pow(x - a, 2) + pow(y - b, 2));
}
//grid view
void showGrid()
{
ClearScreen();
for (unsigned int i = 0; i < GridSize; i++)
{
for (int j = 0; j < GridSize; j++)
{
std::cout << grid[i][j];
std::cout << " ";
}
std::cout << endl;
}
}
//function to place characters, modified as suggested
void placement(int x, int y, char c = '@')
{
grid[x][y] = c;
}
//function to move the user's characters that I can't separate from the computer's movements
void move()
{
srand(time(0));
int x;
int y;
double x_comput;
double y_comput;
double distarray[8];
double dist;
double dista;
double distb;
int xX;
int yY;
int xxX;
int yyY;
memset(grid, '.', sizeof(grid));
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
DWORD NumInputs = 0;
DWORD InputsRead = 0;
INPUT_RECORD irInput;
GetNumberOfConsoleInputEvents(hInput, &NumInputs);
ReadConsoleInput(hInput, &irInput, 1, &InputsRead);
switch (irInput.Event.KeyEvent.wVirtualKeyCode)
{
case VK_LEFT:
y = y - 1;
placement(x, y);
//test for trying to keep a minimum distance between characters
do { xX = random(1, 25);
yY = random(1, 25);
dist = distanceBetweenTwoPoints(x, y, xX, yY);
} while (dist > 6 && dist > 2);
do { xxX = random(1, 25);
yyY = random(1, 25);
dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);
placement(xX, yY, 'X');
placement(xxX, yyY, 'Y');
showGrid();
break;
case VK_UP:
x = x - 1;
placement(x, y);
//test for trying to keep a minimum distance between characters
do { xX = random(1, 25);
yY = random(1, 25);
dist = distanceBetweenTwoPoints(x, y, xX, yY);
} while (dist > 6 && dist > 2);
do { xxX = random(1, 25);
yyY = random(1, 25);
dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);
placement(xX, yY, 'X');
placement(xxX, yyY, 'Y');
showGrid();
break;
case VK_RIGHT:
y = y + 1;
placement(x, y);
//test for trying to keep a minimum distance between characters
do { xX = random(1, 25);
yY = random(1, 25);
dist = distanceBetweenTwoPoints(x, y, xX, yY);
} while (dist > 6 && dist > 2);
do { xxX = random(1, 25);
yyY = random(1, 25);
dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);
placement(xX, yY, 'X');
placement(xxX, yyY, 'Y');
showGrid();
break;
case VK_DOWN:
x = x + 1;
placement(x, y);
//test for trying to keep a minimum distance between characters
do { xX = random(1, 25);
yY = random(1, 25);
dist = distanceBetweenTwoPoints(x, y, xX, yY);
} while (dist > 6 && dist > 2);
do { xxX = random(1, 25);
yyY = random(1, 25);
dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);
placement(xX, yY, 'X');
placement(xxX, yyY, 'Y');
showGrid();
break;
}
placement(x, y);
//display of distances to check at runtime
cout << dist << endl;
cout << dista << endl;
cout << distb << endl;
}
int main(int argc, char *argv[])
{
bool running = true;
while (running)
{
move();
}
}
|