Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include "raylib.h"
- #include<string>
- #define WinHeight 600
- #define WinWidth 800
- #define BoardHeight 300
- #define BoardWidth 300
- #define fps 60
- #define X 1
- #define O -1
- #define BackgroundColor WHITE
- #define LineColor BLACK
- #define CellSize 100
- #define LineThickness 5
- #define WinCenterX WinWidth/2
- #define WinCenterY WinHeight/2
- using namespace std;
- struct vec2 {
- int x, y;
- vec2():x(),y(){}
- vec2(int x, int y) :x(x), y(y) {}
- };
- struct clickzone {
- int cx,cy,x1, x2, y1, y2,id,val;
- //shapes could be added to this, just does a square for now
- clickzone():cx(), cy(), x1(), x2(), y1(), y2(),id(),val(0) {}
- clickzone(int centerx, int centery,int size,int move):cx(centerx),cy(centery),x1(),x2(),y1(),y2(),val(0) {
- id = move;
- x1 = centerx - (size * .5);
- x2 = x1 + size;
- y1 = centery - (size * .5);
- y2 = y1 + size;
- }
- bool inzone(int x, int y) {
- return x > x1 - 1 && x<x2 + 1 && y>y1 - 1 && y < y2 + 1 ? true : false;
- }
- bool inzone(vec2 v) {
- return inzone(v.x, v.y);
- }
- };
- struct ttt {
- int board[3][3];//not used
- vector<clickzone> zones;
- bool player1, player2; // not used
- int hwnd;// not used
- ttt() : hwnd{}, player1{}, player2{}, board{}, zones{} {
- InitWindow( WinWidth, WinHeight, "Tic-Tac-Toe");
- SetTargetFPS(fps);
- }
- void getzones() {
- int move = 1;
- for (int fyz = WinCenterY - CellSize; fyz < WinCenterY + (CellSize * 2); fyz += CellSize) {
- for (int fxz = WinCenterX - CellSize; fxz < WinCenterX + (CellSize * 2); fxz += CellSize) {
- zones.emplace_back(fxz, fyz, CellSize, move);
- move++;
- }
- }
- }
- clickzone* click(vec2 pos) {
- for (auto& i : zones) {
- if (i.inzone(pos)) {
- return &i;
- }
- }
- return nullptr;
- }
- void drawParallelLines(int x, int y, int length, int line_thickness,int width,bool horizontal, Color linecolor) {
- for (int i = 0; i < 2; i++) {
- for (int j = 0; j < line_thickness; j++) {
- if (horizontal) {
- if (i == 0) {
- DrawLine(x, y + j, x + length, y + j, linecolor);
- }
- else {
- DrawLine(x, y + width + j, x + length, y + width + j, linecolor);
- }
- }
- else {
- if (i == 0) {
- DrawLine(x + j, y, x + j, y + length, linecolor);
- }
- else {
- DrawLine(x + j + width, y, x + j + width, y + length, linecolor);
- }
- }
- }
- }
- }
- void grid() {
- drawParallelLines(WinCenterX-(BoardWidth*.5), WinCenterY-(CellSize*.5) , BoardWidth, LineThickness, CellSize, true, LineColor);
- drawParallelLines(WinCenterX-(CellSize*.5), WinCenterY - (BoardHeight*.5), BoardHeight, LineThickness, CellSize, false, LineColor);
- }
- void DrawO(int x, int y, int radius, int thickness, Color linecolor) {
- DrawCircle(x, y, radius, linecolor);
- DrawCircle(x, y, radius-thickness, BackgroundColor);
- }
- void DrawX(int x, int y, int height, int thickness, Color linecolor) {
- int len = sqrt((height * height) + (height * height));
- int x1 = x - (height * .5);
- int x2 = x1 + height;
- int y1 = y - (height * .5);
- int y2 = y1 + height;
- for (int i = 0; i < thickness; i++) {
- DrawLine(x1 + i, y1, x2 + i, y2, linecolor);
- DrawLine(x2 + i, y1, x1 + i, y2, linecolor);
- }
- }
- void DrawBoard() {
- for (int i = 0; i < 9; i++) {
- if (zones[i].val == X) {
- DrawX(zones[i].cx, zones[i].cy, CellSize * .8, LineThickness, LineColor);
- }
- else if (zones[i].val == O) {
- DrawO(zones[i].cx, zones[i].cy, CellSize * .4, LineThickness, LineColor);
- }
- }
- }
- void play2() {
- int win = 0;
- while (!WindowShouldClose())
- { BeginDrawing();
- ClearBackground(BackgroundColor);
- DrawText("Two Players only", 100, 10, 50, LineColor);//change to player select
- DrawText("Press Enter to Continue", 125, 100, 30, LineColor);
- EndDrawing();
- if (GetKeyPressed() == 257) {
- break;
- }
- }
- getzones();
- bool flip = true;
- while (!WindowShouldClose())
- {
- BeginDrawing();
- ClearBackground(BackgroundColor);
- grid();
- DrawBoard();
- if (!win) {
- string s("Player ");
- s.append(flip ? "X" : "O");
- s.append("'s Turn");
- DrawText(s.c_str(), 200, 40, 45, LineColor);
- }
- else {
- string s("Player ");
- s.append(win == X ? "X" : "O");
- s.append(" WINS!!!!!");
- DrawText(s.c_str(), 200, 40, 45, LineColor);
- linewin();
- }
- EndDrawing();
- if ((IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) && !win) {
- auto p = GetMousePosition();
- auto z = click(vec2((int)p.x, (int)p.y));
- if (z && z->val == 0) {
- z->val = flip ? X : O;
- flip = !flip;
- win = checkwin2();
- }
- }
- }
- }
- void play() { // no longer used
- int numberPlayers = 2;
- grid();
- display();
- int turns = 0;
- bool flip = false;
- while (checkwin() == ' ' && turns < 9 && !WindowShouldClose()) {
- int move = 0;
- if (numberPlayers == 2) {
- while (move == 0) {
- std::cout << "Player " << flip + 1 << " Enter A Move \n";
- scanf_s("%d", &move);
- if (checkmove(move)) {
- updateboard(move, flip + 1);
- display();
- }
- else {
- std::cout << "Incorrect input or square is already occupied Please Try again\n";
- move = 0;
- }
- }
- }
- else if (numberPlayers == 1) {
- //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- }
- flip = !flip;
- turns++;
- }
- if (checkwin() == ' ') {
- std::cout << " ITS A TIE\n";
- }
- else {
- std::cout << checkwin() << " WINS!!\n";
- }
- std::cout << "Game Over\n";
- }
- std::vector<int> copyboard() { // this was for the ai might still be used
- std::vector<int> cc(9);
- for (int i = 0; i < 9; i++) {
- cc[i] = board[(int)floor(i / 3)][i % 3];
- }
- return cc;
- }
- int aimove() {
- std::vector<int> cc = copyboard();
- std::vector<int> pms = potentialmoves(cc);
- if (pms.size() == 9) { return 5; }
- std::vector<std::vector<int>> scorelist(pms.size());
- int move = 0;
- return move;
- }
- void aimove_recur(std::vector<int> cc, int* move,bool is_max) {
- }
- std::vector<int> potentialmoves(std::vector<int> cc) {
- //basically empty squares
- std::vector<int> v{};
- for (int i = 0; i < 9; i++) {
- if (cc[i] == 0) {
- v.push_back(i);
- }
- }
- return v;
- }
- //int checktwos(std::vector<int> cc,int opponent) {
- // int xscore = 0;
- // int yscore = 0;
- // int dscore = 0;
- // int xdscore = 0;
- // for (int i = 0; i < 3; i++) {
- // for (int j = 0; j < 3; j++) {
- // xscore += cc[ i * 3 + j ];
- // yscore += cc[ j * 3 + i];
- // if (i == j) {
- // dscore += cc[ (j + i) * 2];
- // xdscore += cc[j + i + 2];
- // }
- // }
- // if (xscore == 2 || yscore == 2 || dscore == 2 || xdscore==2) {
- // return 'X';
- // }
- // xscore = 0;
- // yscore = 0;
- // }
- //}
- void updateboard(int move, int player) { //no longer used
- board[(int)floor((move - 1) / 3)][(move - 1) % 3] = player == 1 ? X : O;
- }
- bool checkmove(int move) { //no longer used
- auto b = board[(int)floor((move-1) / 3)][(move - 1) % 3];
- if (b == 0) { return true; }
- return false;
- }
- void display() { //no longer used
- std::cout << "\n\n\n";
- for (int i = 0; i < 9; i++) {
- auto b = board[(int)floor(i / 3)][i % 3];
- if (b == X) {
- std::cout << "X ";
- }
- else if (b == O) {
- std::cout << "O ";
- }
- else {
- std::cout << i + 1 << " ";
- }
- if (!((i + 1) % 3)) {
- std::cout << '\n';
- }
- }
- std::cout << "\n\n\n";
- }
- void reset() { //pointless
- for (int i = 0; i < 3;i++) {
- for (int j = 0; j < 3; j++) {
- board[i][j] = 0;
- }
- }
- }
- char checkwin() { //no longer used
- int xscore = 0;
- int yscore = 0;
- int dscore = 0;
- int ddscore = 0;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- xscore +=board[i][j];
- yscore += board[j][i];
- if (i == j) {
- dscore += board[i][j];
- if (i == 0) {
- ddscore += board[i + 2][j];
- }
- else if (i == 1) {
- ddscore += board[i][j];
- }
- else if (i == 2) {
- ddscore += board[i][j - 2];
- }
- }
- }
- if (xscore == 3 || yscore==3 || dscore==3 || ddscore == 3) {
- return 'X';
- }
- else if (xscore == -3 || yscore == -3 || dscore == -3 || ddscore == -3) {
- return 'O';
- }
- xscore = 0;
- yscore = 0;
- }
- return ' ';
- }
- int checkwin2() {
- int h1 = zones[0].val + zones[1].val + zones[2].val;
- int h2 = zones[3].val + zones[4].val + zones[5].val;
- int h3 = zones[6].val + zones[7].val + zones[8].val;
- int v1 = zones[0].val + zones[3].val + zones[6].val;
- int v2 = zones[1].val + zones[4].val + zones[7].val;
- int v3 = zones[2].val + zones[5].val + zones[8].val;
- int d1 = zones[0].val + zones[4].val + zones[8].val;
- int d2 = zones[2].val + zones[4].val + zones[6].val;
- if (h1 == 3 || h2 == 3 || h3 == 3 || v1 == 3 || v2 == 3 || v3 == 3 || d1 == 3 || d2 == 3) {
- return X;
- }
- else if (h1 == -3 || h2 == -3 || h3 == -3 || v1 == -3 || v2 == -3 || v3 == -3 || d1 == -3 || d2 == -3) {
- return O;
- }
- return 0;
- }
- void linewin() {
- int h1 = zones[0].val + zones[1].val + zones[2].val;
- int h2 = zones[3].val + zones[4].val + zones[5].val;
- int h3 = zones[6].val + zones[7].val + zones[8].val;
- int v1 = zones[0].val + zones[3].val + zones[6].val;
- int v2 = zones[1].val + zones[4].val + zones[7].val;
- int v3 = zones[2].val + zones[5].val + zones[8].val;
- int d1 = zones[0].val + zones[4].val + zones[8].val;
- int d2 = zones[2].val + zones[4].val + zones[6].val;
- if (h1 == 3 || h1 == -3) {
- drawlinethick(zones[0].x1, zones[0].cy, zones[2].x2, zones[0].cy, LineThickness, RED);
- }
- else if (h2 == 3 || h2 == -3) {
- drawlinethick(zones[3].x1, zones[3].cy, zones[5].x2, zones[3].cy, LineThickness, RED);
- }
- else if (h3 == 3 || h3 == -3) {
- drawlinethick(zones[6].x1, zones[6].cy, zones[8].x2, zones[6].cy, LineThickness, RED);
- }
- else if (v1 == 3 || v1 == -3) {
- drawlinethick(zones[0].cx, zones[0].y1, zones[0].cx, zones[6].y2, LineThickness, RED);
- }
- else if (v2 == 3 || v2 == -3) {
- drawlinethick(zones[1].cx, zones[1].y1, zones[1].cx, zones[7].y2, LineThickness, RED);
- }
- else if (v3 == 3 || v3 == -3) {
- drawlinethick(zones[2].cx, zones[2].y1, zones[2].cx, zones[8].y2, LineThickness, RED);
- }
- else if (d1 == 3 || d1 == -3) {
- drawlinethick(zones[0].x1, zones[0].y1, zones[8].x2, zones[8].y2, LineThickness, RED);
- }
- else if (d2 == 3 || d2 == -3) {
- drawlinethick(zones[2].x2, zones[2].y1, zones[6].x1, zones[6].y2, LineThickness, RED);
- }
- }
- void drawlinethick(int x1, int y1, int x2, int y2, int thickness, Color linecolor) {
- for (int i = 0; i < thickness; i++) {
- if (y1 == y2) {
- DrawLine(x1, y1 + i, x2, y2 + i, linecolor);
- }
- else {
- DrawLine(x1 +i, y1, x2+i, y2, linecolor);
- }
- }
- }
- };
- int main() {
- ttt a;
- a.play2();
- }
Advertisement
Add Comment
Please, Sign In to add comment