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
|
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
//variables and constants
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
const int WIDTH = 10;
const int HEIGHT = 7;
const int BOARD_DIM = 4;
const int NUM_PLAYERS = 2;
const int WIN_LENGTH = 3;
enum TileType{EMPTY, NOUGHT, CROSS};
struct Tile{bool Selected; TileType type;};
struct Point{int x; int y;};
struct Player{TileType side; bool Playing;};
//functions
void RunGame(Tile board[][BOARD_DIM], Player players[]);
bool Input(Tile board[][BOARD_DIM], Player players[], Point *Selected);
void Init(Tile board[][BOARD_DIM], Player players[]);
TileType CheckWinConditions(Tile board[][3]);
void Draw(TileType type, int x, int y);
void DrawBoard(Tile board[][BOARD_DIM]);
void DrawBox(int style, int col, int row, int length,int amount, bool fill);
void gotoXY(int x, int y);
void gotoXY(int x, int y, string text);
void ClearScreen();
int main()
{
Tile board[BOARD_DIM][BOARD_DIM];
Player players[NUM_PLAYERS];
while(true)
{
Init(board, players);
RunGame(board, players);
}
return 0;
}
void RunGame(Tile board[][BOARD_DIM], Player players[])
{
bool done = false;
bool Render = false;
Point Selected = {0,0};
TileType Winner = EMPTY;
while(!done)
{
Render = Input(board, players, &Selected);
Winner = CheckWinConditions(board);
if (Winner == NOUGHT || Winner == CROSS)
done = true;
if (Render)
{
DrawBoard(board);
Render = false;
}
}
if (Winner == NOUGHT)
cout << "Noughts Win" << endl;
else
cout << "Crosses WIN" << endl;
}
+bool Input(Tile board[][BOARD_DIM], Player players[], Point *Selected){}
+void Init(Tile board[][BOARD_DIM], Player players[]){}
TileType CheckWinConditions(Tile board[][BOARD_DIM])
{
TileType Winner = EMPTY;
int rX, cX, dX, rO, cO, dO;
for (int x = 0; x < BOARD_DIM; x++)
{
rX=0;
cX=0;
dX=0;
rO=0;
cO=0;
dO=0;
for (int y = 0; y < BOARD_DIM; y++)
{
if(board[x][y].type == CROSS)
rX++;
if(board[x][y].type == NOUGHT)
rO++;
if(board[y][x].type == CROSS)
cX++;
if(board[y][x].type == NOUGHT)
cO++;
if(board[y][y].type == CROSS || board[BOARD_DIM-1-y][y].type == CROSS)
dX++;
if(board[y][y].type == NOUGHT || board[BOARD_DIM-1-y][y].type == NOUGHT)
dO++;
if (rO==WIN_LENGTH || cO==WIN_LENGTH || dO==WIN_LENGTH)
{
Winner = NOUGHT;
break;
}
else if (rX==WIN_LENGTH || cX==WIN_LENGTH || dX==WIN_LENGTH)
{
Winner = CROSS;
break;
}
}
}
return Winner;
}
+void Draw(TileType type, int x, int y){}
void DrawBoard(Tile board[][BOARD_DIM])
{
for (int y = 0; y < BOARD_DIM; y++)
{
for (int x = 0; x < BOARD_DIM; x++)
{
if (board[x][y].Selected == true && board[x][y].type == EMPTY)
DrawBox(1,x*WIDTH,y*HEIGHT,WIDTH,HEIGHT,1);
else
DrawBox(1,x*WIDTH,y*HEIGHT,WIDTH,HEIGHT,0);
if (board[x][y].type == CROSS)
Draw(CROSS, x, y);
else if (board[x][y].type == NOUGHT)
Draw(NOUGHT, x, y);
}
}
}
+void DrawBox(int style, int col, int row, int length,int amount, bool fill){}
+void gotoXY(int x, int y){}
+void gotoXY(int x, int y, string text){}
+void ClearScreen(){}
|