반응형

#include "stdafx.h"
#include <stdio.h>
#include "myfunc.h"
#include <random>
using namespace std;
#include <conio.h>

#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define SPACE 32


int main()
{
 int board[3][3] = { 0 };
 int count = 0;
 int x = 0, y = 0;
 int inputkey = 0;
 int tmp = 0;
 

 for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
   count++;
   board[i][j] = count;
  }
 }
 
 while (1) {
  
  system("cls");
  
  printf(" ────────\n");
  for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
    setColor(BLACK, WHITE);
    printf("│");
    if (board[i][j] == 1) {
     x = i; y = j;
     setColor(RED, WHITE);
    }
    else {
     setColor(BLACK, WHITE);
    }
    printf("%2d", board[i][j]);
    setColor(BLACK, WHITE);
    printf("│");
   }
   printf("\n ────────\n");
  }
  Sleep(50);

  inputkey = _getch();

  
  

 }
 
 return 0;
}

 

// 미완성

=================================================================================================

//완성 소스

#include "pch.h"
#include "myFunc.h"
#include <stdlib.h>
#include <conio.h>
#include <random>

using namespace std;

#define MAX 3
#define LE 75
#define RI 77
#define UP 72
#define DO 80
#define SPACE 32

#define ME 0
#define COM 1

int main()
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(0, MAX - 1);

 // tic-tac-toe variables
 int board[MAX][MAX] = {0,};
 int cursor_x = 0, cursor_y = 0;
 int com_x = 0, com_y = 0;
 int inputKey = 0;
 int turn = 0;
 
 while (1)
 {
  // Drawing
  system("cls");
  setColor(BLACK, WHITE);
  printf("\n  ┌───┬───┬───┐\n");
  for (int y = 0; y < MAX; y++)
  {   
   printf("  │");
   for (int x = 0; x < MAX; x++)
   {
    if (x == cursor_x && y == cursor_y)
    {
     setColor(BLACK, RED);
     if (board[y][x] == 0) printf(" ↖");
     else if (board[y][x] == 1) printf(" ○");
     else if (board[y][x] == 2) printf(" Ⅹ");
     setColor(BLACK, WHITE);
    }
    else if (board[y][x] == 0) printf("   ");
    else if (board[y][x] == 1) printf(" ○");
    else if (board[y][x] == 2) printf(" Ⅹ");
    if (x < (MAX - 1)) printf("│");
   }
   printf("│");
   if (y < (MAX - 1)) printf("\n  ├───┼───┼───┤\n");
  }
  printf("\n  └───┴───┴───┘\n");  

  // Game Checking


  // input (Player Turn)
  inputKey = _getch();
  if (inputKey == LE) cursor_x--;
  else if (inputKey == RI) cursor_x++;
  else if (inputKey == UP) cursor_y--;
  else if (inputKey == DO) cursor_y++;
  else if (inputKey == SPACE)
  {
   if (turn == ME)
   {
    if (board[cursor_y][cursor_x] == 0)
    {
     board[cursor_y][cursor_x] = 1;
     turn = COM;
    }
   }
  }

  // COM Turn
  if (turn == COM)
  {
   int comDone = 1;
   while (comDone)
   {
    com_x = range(rn);
    com_y = range(rn);
    if (board[com_y][com_x] == 0)
    {
     board[com_y][com_x] = 2;
     comDone = 0;
     turn = ME;
    }
   }   
  } 
 }
}

반응형

'Study > C' 카테고리의 다른 글

C언어 - 비트연산2 (1을 이동시키기)  (0) 2018.08.29
C언어 - 비트 연산  (0) 2018.08.29
C언어 - 블럭 옴기기 게임  (0) 2018.08.28
C언어 - 배열 포인터  (0) 2018.08.28
C언어 - 포인터 2  (0) 2018.08.28

+ Recent posts