Login
Order Now
Support
theprogrammingassignmenthelp

C++ Program: Tic Tak Toe

  • 27th Feb, 2019
  • 16:31 PM

Please find below the C++ Program for Tic Tak Toe game. If you need help in coding a C++ game then reach out to our customer care.

/*
    Remember to compile try:
        g++ tictactoe.cpp -o tictactoe -I/usr/X11R6/include -I/usr/X11R6/include/X11 -L/usr/X11R6/lib -L/usr/X11R6/lib/X11 -lX11
*/
/* include the X library headers */
#include < X11 / Xlib.h >
#include < X11 / Xutil.h >
#include < X11 / Xos.h >
#include < stdio.h >
#include < stdlib.h >

  Display * dis;
int screen;
Window win;
GC gc;

void init_x();
void close_x();
void redraw();
int checkForResult(int ticToeMatrix[3][3]);

int main() {
  XEvent event; /* the XEvent declaration */
  char text[255];
  int result;
  init_x();
  XNextEvent(dis, & event);
  XDrawRectangle(dis, win, gc, 50, 50, 30, 30);
  XDrawRectangle(dis, win, gc, 80, 50, 30, 30);
  XDrawRectangle(dis, win, gc, 110, 50, 30, 30);
  XDrawRectangle(dis, win, gc, 50, 80, 30, 30);
  XDrawRectangle(dis, win, gc, 80, 80, 30, 30);
  XDrawRectangle(dis, win, gc, 110, 80, 30, 30);
  XDrawRectangle(dis, win, gc, 50, 110, 30, 30);
  XDrawRectangle(dis, win, gc, 80, 110, 30, 30);
  XDrawRectangle(dis, win, gc, 110, 110, 30, 30);
  int chance = 0;
  int numberOfClicks = 0;
  int ticToeMatrix[3][3] = {
    0
  };
  /* check events forever... */
  while (1) {
    XNextEvent(dis, & event);
    /* Check for Button Press event */
    if (event.type == ButtonPress) {
      int x = event.xbutton.x,
        y = event.xbutton.y;
      int i, j;
      if (x < 140 && x > 50 && y < 140 && y > 50) {
        if (x > 110) {
          x = 122;
          i = 0;
        } else if (x > 80) {
          x = 92;
          i = 1;
        } else if (x > 50) {
          x = 62;
          i = 2;
        }
        if (y > 110) {
          y = 132;
          j = 0;
        } else if (y > 80) {
          y = 102;
          j = 1;
        } else if (y > 50) {
          y = 72;
          j = 2;
        }
        if (ticToeMatrix[i][j] == 0) {
          chance == 0 ? strcpy(text, "X") : strcpy(text, "O");
          numberOfClicks++;
          chance = !chance;
          ticToeMatrix[i][j] = chance + 1;

          XDrawString(dis, win, gc, x, y, text, strlen(text));
          result = checkForResult(ticToeMatrix);
          if (result == 1) {
            strcpy(text, "O WINS");
            XDrawString(dis, win, gc, 50, 40, text, strlen(text));
            break;
          } else if (result == 2) {
            strcpy(text, "X WINS");
            XDrawString(dis, win, gc, 50, 40, text, strlen(text));
            break;
          } else if (numberOfClicks == 9) {
            strcpy(text, "DRAW!!!");
            XDrawString(dis, win, gc, 50, 40, text, strlen(text));
            break;
          }
        }
      }
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      printf(" %d", ticToeMatrix[i][j]);
    }
  }
  XNextEvent(dis, & event);
}

int checkForResult(int ticToeMatrix[3][3]) {
  int count = 0;
  /* check for O to win */
  for (int i = 0; i < 3; i++) {
    count = 0;
    for (int j = 0; j < 3; j++) {
      if (ticToeMatrix[i][j] == 1) {
        count++;
      }
    }
    if (count == 3) {
      return 1;
    }
  }
  count = 0;
  for (int i = 0; i < 3; i++) {
    count = 0;
    for (int j = 0; j < 3; j++) {
      if (ticToeMatrix[j][i] == 1) {
        count++;
      }
    }
    if (count == 3) {
      return 1;
    }
  }
  count = 0;
  for (int i = 0; i < 3; i++) {
    if (ticToeMatrix[i][i] == 1) {
      count++;
    }
  }
  if (count == 3) {
    return 1;
  }
  count = 0;
  for (int i = 0; i < 3; i++) {
    if (ticToeMatrix[i][2 - i] == 1) {
      count++;
    }
  }
  if (count == 3) {
    return 1;
  }
  /* check for X to win */
  count = 0;
  for (int i = 0; i < 3; i++) {
    count = 0;
    for (int j = 0; j < 3; j++) {
      if (ticToeMatrix[i][j] == 2) {
        count++;
      }
    }
    if (count == 3) {
      return 2;
    }
  }
  count = 0;
  for (int i = 0; i < 3; i++) {
    count = 0;
    for (int j = 0; j < 3; j++) {
      if (ticToeMatrix[j][i] == 2) {
        count++;
      }
    }
    if (count == 3) {
      return 2;
    }
  }
  count = 0;
  for (int i = 0; i < 3; i++) {
    if (ticToeMatrix[i][i] == 2) {
      count++;
    }
  }
  if (count == 3) {
    return 2;
  }
  count = 0;
  for (int i = 0; i < 3; i++) {
    if (ticToeMatrix[i][2 - i] == 2) {
      count++;
    }
  }
  if (count == 3) {
    return 2;
  }
  /* for draw*/
  return 0;

}

void init_x() {
  /* get the colors black and white for initilization */
  unsigned long black, white;

  dis = XOpenDisplay((char * ) 0);
  screen = DefaultScreen(dis);
  black = BlackPixel(dis, screen),
    white = WhitePixel(dis, screen);
  win = XCreateSimpleWindow(dis, DefaultRootWindow(dis), 0, 0,
    300, 300, 5, black, white);
  XSetStandardProperties(dis, win, "TIC-TAC-TOE", "", None, NULL, 0, NULL);
  XSelectInput(dis, win, ExposureMask | ButtonPressMask | KeyPressMask);
  gc = XCreateGC(dis, win, 0, 0);
  XSetBackground(dis, gc, white);
  XSetForeground(dis, gc, black);
  XClearWindow(dis, win);
  XMapRaised(dis, win);
}

void close_x() {
  XFreeGC(dis, gc);
  XDestroyWindow(dis, win);
  XCloseDisplay(dis);
  exit(1);
}

void redraw() {
  XClearWindow(dis, win);
}

Share this post