1차로 만든 오목
구현내용:
1. 15*15 판 (좌표가 보임)
2. 5개가 연달아 놓여있으면 승
3. 이미 놓여있는 곳에 놓으면 다시 놓아야함
버그가 있을 지도 모름.
main.cpp
//Publivate
#include "board.h"
int main(void)
{
Board game1; //make a game
game1.black_player.set_color('B'); //player1 is B(black)
game1.white_player.set_color('W'); //player2 is W(white)
game1.print_map(); //print first map
while (1) //infinite loop
{
game1.black_player.set_pointer(); //black turn
game1.set_map(game1.black_player.get_x_point(), game1.black_player.get_y_point()); //black place stone in map
game1.print_map(); //print map
if (game1.game_condition(game1.black_player.get_color()) == 1) //balck win?
break;
game1.white_player.set_pointer(); //black turn
game1.set_map(game1.white_player.get_x_point(), game1.white_player.get_y_point()); //black place stone in map
game1.print_map(); //print map
if (game1.game_condition(game1.white_player.get_color()) == 1) //balck win?
break;
}
system("pause");
return 0;
}
board.h
#pragma once
#include "player.h"
const int max = 15;
class Board //오목 class
{
public:
Board(); //생성자
void set_map(int x, int y); //돌 두기
void print_map(); //오목판 출력
Player black_player; //black_player
Player white_player; //White_player
int game_condition(char c); //who is win or draw?
private:
char map[max][max]; //오목판
int count; //몇 수인 지
bool is_empty(int x, int y); //Its space is empty?
int horizontal_win(char c); //가로승
int vertical_win(char c); //세로승
int right_diagonal_win(char c); //오른쪽아래 대각선 승
int left_diagonal_win(char c); //왼쪽아래 대각선 승
int draw(); //is draw?
int win_count; //if win_count is 5, game is over
};
board.cpp
#include "board.h"
#define win_max 5
#define blank '+'
Board::Board() //오목 constructor
{
system("clear");
for (int i = 0; i < max; i++) //initialize 오목 Board
for (int j = 0; j < max; j++)
{
map[j][i] = blank; //fill the blank
}
count = 0;
}
void Board::set_map(int x, int y) //set stone at the map
{
if (is_empty(x,y) == true) //if black
{
if(count%2 == 0)
map[x][y] = black_player.get_color(); //B is Black stone
else
map[x][y] = white_player.get_color(); //W is White stone
count++;
}
else //if not blank
{
cout << "이미 돌이 놓여져 있습니다." << endl; //print error message
if (count % 2 == 0)
{
black_player.set_pointer();
set_map(black_player.get_x_point(), black_player.get_y_point());
}
else
{
white_player.set_pointer();
set_map(white_player.get_x_point(), white_player.get_y_point());
}
}
}
void Board::print_map() //print map
{
int row = 1; //row (1~15)
char column = 'a'; //column (a~
system("cls");
cout << " ";
for (int i = 0; i < max; i++)
{
cout << setw(2) << column;
column++;
}
cout << endl;
for (int i = 0; i < max; i++)
{
cout << setw(2) << row << setw(4);
row++;
for (int j = 0; j < max; j++)
{
cout << setw(2) << map[j][i]; //print map
}
cout << endl;
}
}
bool Board::is_empty(int x, int y) //if balnk, empty
{
if (map[x][y] == blank)
return true;
else
return false;
}
int Board::game_condition(char c) //4 win condition, 1 draw condition
{
if (horizontal_win(c) == 1) //가로 win
{
cout << c << " is win" << endl;
return 1;
}
else if (vertical_win(c) == 1) //세로 win
{
cout << c << " is win" << endl;
return 1;
}
else if (right_diagonal_win(c) == 1) //오른쪽 아래 대각선 win
{
cout << c << " is win" << endl;
return 1;
}
else if (left_diagonal_win(c) == 1) //왼쪽 아래 대각선 win
{
cout << c << " is win" << endl;
return 1;
}
else if (draw() == 1) //draw
{
cout << "draw" << endl;
return 1;
}
else
return 0;
}
int Board::horizontal_win(char c) //가로승
{
win_count = 0;
for (int i = 0; i < max; i++)
{
for (int j = 0; j < max-5; j++)
{
if (map[j][i] == c)
{
for (int k = 1; k < win_max; k++) //가로로 승리
{
if (map[j + k][i] == c)
{
win_count++;
if (win_count == 5)
{
return 1;
}
}
else
{
win_count = 0;
return 0;
}
}
}
}
}
}
int Board::vertical_win(char c) //세로승
{
win_count = 0;
for (int i = 0; i < max-5; i++)
{
for (int j = 0; j < max; j++)
{
if (map[j][i] == c)
{
for (int k = 1; k < win_max; k++) //가로로 승리
{
if (map[j][i + k] == c)
{
win_count++;
if (win_count == 5)
{
return 1;
}
}
else
{
win_count = 0;
return 0;
}
}
}
}
}
}
int Board::right_diagonal_win(char c) //오른쪽아래 대각선 승
{
win_count = 0;
for (int i = 0; i < max-5; i++)
{
for (int j = 0; j < max-5; j++)
{
if (map[j][i] == c)
{
for (int k = 1; k < win_max; k++) //가로로 승리
{
if (map[j + k][i + k] == c)
{
win_count++;
if (win_count == 5)
{
return 1;
}
}
else
{
win_count = 0;
return 0;
}
}
}
}
}
}
int Board::left_diagonal_win(char c) //왼쪽아래 대각선 승
{
win_count = 0;
for (int i = 0; i < max-5; i++)
{
for (int j = 4; j < max; j++)
{
if (map[j][i] == c)
{
for (int k = 1; k < win_max; k++) //가로로 승리
{
if (map[j - k][i + k] == c)
{
win_count++;
if (win_count == 5)
{
return 1;
}
}
else
{
win_count = 0;
return 0;
}
}
}
}
}
}
int Board::draw() //is draw
{
if (count == 225)
return 1;
else
return 0;
}
player.h
#pragma once
#include <iostream>
#include <iomanip>
using namespace std;
class Player
{
private:
int x_point; //x point
int y_point; //y point
char color; //B or W
public:
void set_pointer();
void set_color(char c);
char get_color();
int get_x_point();
int get_y_point();
};
player.cpp
#include "player.h"
void Player::set_pointer()
{
char x; //x pointer
int y; //y pointer
int temp_count;
while (1)
{
temp_count = 0;
cout << "x(a~m), y(1~15) 를 띄어서 입력해주세요." << endl;
cin >> x >> y; //input x,y
if (x >= 'a' && x <= 'm')
{
x_point = (int)x - 97;
temp_count++;
}
else if (x >= 'A' && x <= 'M')
{
x_point = (int)x - 65;
temp_count++;
}
if (y >= 1 && y <= 15)
{
y_point = y - 1;
temp_count++;
if (temp_count == 2)
break;
}
}
}
void Player::set_color(char c)
{
color = c;
}
char Player::get_color()
{
return color;
}
int Player::get_x_point()
{
return x_point;
}
int Player::get_y_point()
{
return y_point;
}