U E D R , A S I H C RSS

Omok/재니

작성자의 페이지

02 장재니 Genie

소스

Turbo C

~cpp 
#include <iostream.h>
#include <conio.h>

void board_display();
void put();
void check();

char board[19][19];
int key, x = 9, y = 9;
char turn = '@';
int winner = 0;

int main()
{
	board_display();
	gotoxy(x*3+1, y+1);
	while(winner == 0)
	{
		gotoxy(x*3+1, y+1);
		key = getch();
		if (key == 0x20)
			if (board[x][y] == '+')
			{
				put();
				check();
				if (winner != 0)
					break;
			}
		if (key == 0x1b)
			return 0;
		if(key == 0x00)
		{
			key = getch();
			if (key == 0x48)
				if (y != 0)
					y--;
			if (key == 0x50)
				if (y != 18)
					y++;
			if (key == 0x4b)
				if (x != 0)
					x--;
			if (key == 0x4d)
				if (x != 18)
					x++;
			continue;
		}
	}
	gotoxy(1,20);
	cout << "Winner is ";
	if(winner == 1)
		cout << "Black!n";
	else if(winner == 2)
		cout << "White!n";
	getch();
	return 0;
}

void board_display()
{
	clrscr();
	for (int i = 0 ; i < 19 ; i++)
	{
		for (int j = 0 ; j < 19 ; j++)
		{
			board[i][j] = '+';
			cout << board[i][j] << "  ";
		}
		cout << endl;
	}
}

void put()
{
	board[x][y] = turn;
	cout << board[x][y];
}

void check()
{
	int cx, cy, num;
	num = 0;
	cx = x, cy = y;
	while (board[cx-1][cy] == turn)
		cx--;
	while (board[cx+1][cy] == turn)
	{
		cx++;
		num++;
	}
	if (num < 4)
	{
		num = 0;
		cx = x, cy = y;
	}
	while (board[cx][cy-1] == turn)
		cy--;
	while (board[cx][cy+1] == turn)
	{
		cy++;
		num++;
	}
	if (num < 4)
	{
		num = 0;
		cx=x, cy=y;
	}
	while (board[cx-1][cy-1] == turn)
	{
		cx--, cy--;
	}
	while (board[cx+1][cy+1] == turn)
	{
		cx++, cy++;
		num++;
	}
	if (num < 4)
	{
		num = 0;
		cx=x, cy=y;
	}
	while (board[cx+1][cy-1] == turn)
	{
		cx++;
		cy--;
	}
	while (board[cx-1][cy+1] == turn)
	{
		cx--;
		cy++;
		num++;
	}
	if (turn == '@')
		turn = 'O';
	else
		turn = '@';
	if (num == 4)
	{
		if (turn == '@')
			winner = 2;
		if (turn == 'O')
			winner = 1;
	}
	winner == 0;
}

C++

~cpp
#include <iostream>
using namespace std;

class Board{
private:
	int m_Board[19][19];
	int turn;
	int x, y;
	int countStone;
	int check_X, check_Y;
public:
	Board(){
		turn = 0;
		for (int i = 0 ; i < 19 ; i++)
			for (int j = 0 ; j < 19 ; j++)
				m_Board[i][j] = 0;
	}
	void showBoard(){
		system("cls");
		for (int i = 0 ; i < 19 ; i++){
			for (int j = 0 ; j < 19 ; j++)
				cout << m_Board[i][j] << " ";
			cout << endl;
		}
	}
	void inputPosition(){
		cout << "Input the position (x, y) : ";
		cin >> x >> y;
		if (x < 0 || x >= 19 || y < 0 || y >= 19){
			cout << "잘못 입력하셨습니다.\n";
			system("pause");
		}
		else if (m_Board[x][y] != 0){
			cout << "이미 놓은 자리입니다.\n";
			system("pause");
		}
		else {
			m_Board[x][y] = (turn % 2) + 1;
			turn++;
		}
	}
	bool checkOmok(){
		int checkDirection[4][2] = {{1,1},{1,0},{0,1},{-1,1}};
		for (int i = 0 ; i < 4 ; i++){
			countStone = 0;
			for (int j = 0 ; j < 2 ; j++){
				checkDirection[i][0] *= -1, checkDirection[i][1] *= -1;
				check_X = x + checkDirection[i][0];
				check_Y = y + checkDirection[i][1];
				while (m_Board[check_X][check_Y] == (turn % 2)){
					countStone++;
					check_X += checkDirection[i][0];
					check_Y += checkDirection[i][1];
				}
			}
			if (countStone == 4)
				return true;
		}
		return false;
	}
	void showEndMsg(){
		cout << turn % 2 << " 승리.\n";
	}
};

int main(){
	Board bd;
	while (!bd.checkOmok()){
		bd.showBoard();
		bd.inputPosition();
	}
	bd.showBoard();
	bd.showEndMsg();
	return 0;
}
----
보드 객체가 너무 만능인거 아냐? ㅋㅋ -창섭
----
Omok
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:53
Processing time 0.0101 sec