본문 바로가기
대학교/2.객체지향프로그래밍_JAVA

Tic_Tac_Toe(틱택토) 게임

by Jcoder 2017. 4. 21.

< 원본 >은 컴퓨터랑 두는건데 수정은 사람끼리 교대로 둘 수 있습니다.


소스 >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
 
public class Tic_Tac_Toe
{
    public static void main(String[] args)
    {
        char [][] board = new char[3][3];
        int x, y;
        
        Scanner scan = new Scanner(System.in);
        
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                board[i][j] = ' ';
            }
        }
        
        do
        {
            for(int i = 0; i < 3; i++)
            {
                System.out.println("  " + board[i][0+ "| "+ board[i][1+ " | "+ board[i][2]);
                if(i != 2)
                {
                System.out.println("---|---|---");
                }
            }
        
            System.out.println("다음 수의 좌표를 입력하시오: ");
            x = scan.nextInt();
            y = scan.nextInt();
 
            if (board[x][y] != ' ')
            {
                System.out.println("잘못된 위치 입니다. ");
                continue;
            }
            else
            {
                board[x][y] = 'X';
            }
            
            for(int i = 0; i < 3; i++)
            {
                System.out.println("  " + board[i][0+ "| "+ board[i][1+ " | "+ board[i][2]);
                if(i != 2)
                {
                System.out.println("---|---|---");
                }
            }
            
            System.out.println("다음 수의 좌표를 입력하시오: ");
            x = scan.nextInt();
            y = scan.nextInt();
 
            if (board[x][y] != ' ')
            {
                System.out.println("잘못된 위치 입니다. ");
                continue;
            }
            else
            {
                board[x][y] = 'O';
            }
        }while(true);
    }
}
cs


원본 >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
 
public class Tic_Tac_Toe
{
    public static void main(String[] args)
    {
        char [][] board = new char[3][3];
        int x, y;
        
        Scanner scan = new Scanner(System.in);
        
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                board[i][j] = ' ';
            }
        }
        
        do
        {
            for(int i = 0; i < 3; i++)
            {
                System.out.println("  " + board[i][0+ "| "+ board[i][1+ " | "+ board[i][2]);
                if(i != 2)
                {
                System.out.println("---|---|---");
                }
            }
        
            System.out.println("다음 수의 좌표를 입력하시오: ");
            x = scan.nextInt();
            y = scan.nextInt();
 
            if (board[x][y] != ' ')
            {
                System.out.println("잘못된 위치 입니다. ");
                continue;
            }
            else
            {
                board[x][y] = 'X';
            }
            
            int i = 0, j = 0;
            for(i = 0; i < 3; i++)
            {
                for(j = 0; j < 3; j++)
                    if (board[i][j] == ' ')
                        break;
                    if (board[i][j] == ' ')
                        break;
            }
            if (i < 3 && j < 3)
            {
                board[i][j] = 'O';
            }
        }while(true);
    }
}
cs

실행결과 >

Tic_Tac_Toe.java