커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
스스로 해결
Console.Write가 작동하지 않는 문제.
C# 문법종합반 v0
2주차
북마크
이*환
댓글
1
추천
0
조회수
25
조회수
25
스스로 해결

* 겪고 있는 문제 상황을 최대한 자세ㅌ하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


2장에서 틱택토 만들기 과제를 하다가 메서드에서 Console.Write가 작동하지 않는 현상이 발생했습니다.


static void checkClear(int a, int b, int c)
    {
        Console.Write("checkClear 작동");
        if ((board[a] == board[b]) && (board[b] == board[c]))
        {
            if (board[a] == 'X')
            {
                Console.WriteLine("플레이어 1의 승리!");
            }
            else
            {
                Console.WriteLine("플레이어 2의 승리!");
            }
            gameOver = true;
        }
    }


   static void FullCheck()
    {
        Console.Write("FullCheck 작동");
        int num=0;
        foreach(char cell in board)
        {
            if(cell == 'O' || cell == 'X')
            {
                num++;
            }
        }
        if(num == 9)
        {
            Console.WriteLine("무승부");
            gameOver = true;
        }
    }


스파르타 즉문즉답



메서드 에서만 Console.Write가 작동하질 않는데 원인을 모르겠습니다.


작성한 코드 및 에러 메세지

전체 코드 입니다.

using System;


class TicTacTic {
    static char[] board = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    static int turn = 1;
    static bool gameOver =false;


    static void Main()
    {
        Console.WriteLine("플레이어 1: X 와 플레이어 2: 0");
        while (!gameOver)
        {
            if (turn % 2 == 0)
            {
                InputAndCheck(2);
            }
            else
            {
                InputAndCheck(1);
            }
         
            turn++;
          
            PrintBoard();


        }
    }


    static void PrintBoard()
    {
        Console.Clear();
        Console.WriteLine();
        Console.WriteLine("     |     |     ");
        Console.WriteLine("  {0}  |  {1}  |  {2}  ", board[7], board[8], board[9]);
        Console.WriteLine("_____|_____|_____");
        Console.WriteLine("     |     |     ");
        Console.WriteLine("  {0}  |  {1}  |  {2}  ", board[4], board[5], board[6]);
        Console.WriteLine("_____|_____|_____");
        Console.WriteLine("     |     |     ");
        Console.WriteLine("  {0}  |  {1}  |  {2}  ", board[1], board[2], board[3]);
        Console.WriteLine("     |     |     ");
    }


    static void InputAndCheck(int p)
    {
        Console.Write("플레이어 "+p+"의 차례 : ");
        char inputNum = Console.ReadKey().KeyChar;
        int arrayNum = int.Parse(inputNum.ToString());
        if (board[arrayNum] == 'O' || board[arrayNum] == 'X')
        {
            Console.WriteLine("\n이미 선택한 칸 입니다. 다시 선택해 주세요.");
            InputAndCheck(p);
        }
        else
        {
            if (p == 2)
            {
                board[arrayNum] = 'O';
            }
            else
            {
                board[arrayNum] = 'X';
            }
        }
        BoardCheckClear();
    }
   static void BoardCheckClear()
    {
        checkClear(1, 2, 3);
        checkClear(4, 5, 6);
        checkClear(7, 8, 9);
        checkClear(1, 5, 9);
        checkClear(3, 5, 7);
        checkClear(1, 4, 7);
        checkClear(2, 5, 8);
        checkClear(3, 6, 9);


        FullCheck();


    }


    static void checkClear(int a, int b, int c)
    {
        Console.Write("checkClear 작동");
        if ((board[a] == board[b]) && (board[b] == board[c]))
        {
            if (board[a] == 'X')
            {
                Console.WriteLine("플레이어 1의 승리!");
            }
            else
            {
                Console.WriteLine("플레이어 2의 승리!");
            }
            gameOver = true;
        }
    }


   static void FullCheck()
    {
        Console.Write("FullCheck 작동");
        int num=0;
        foreach(char cell in board)
        {
            if(cell == 'O' || cell == 'X')
            {
                num++;
            }
        }
        if(num == 9)
        {
            Console.WriteLine("무승부");
            gameOver = true;
        }
    }
}




취소
 공유
취소
댓글 0
댓글 알림
나의얼굴