2014-12-28 7 views
-1

나는 싱글 플레이어 tic tac toe 게임을 만들려고하고있다. 이미 2 명의 플레이어가 온라인에서 찾은 일부 코드를 사용하고 있지만 이미 가지고있는 코드로 단일 플레이어의 코드를 입력하는 행운을 얻지는 못합니다. 나는 당신이 게임을 시작하기 위해 게임 폼 (모든 메인 코드가있는 곳)으로 간다. 1 ~ 2 명의 플레이어를 플레이하고 싶은지 묻는 시작 메뉴가있다. 또한 vb에 익숙하지 않고 아직 모든 용어를 모른다. 그래서 당신이 도와 줄 수 있다면 상세하고 코드에 주석을 남기십시오.싱글 플레이어에서 컴퓨터를 움직이는 방법 tic tac toe game

내가하고 싶은 것은 컴퓨터가 첫 번째 이동을 위해 임의의 선택으로 이동 한 다음 이미 수행 된 것을 바탕으로 컴퓨터에서 선택을 해제하는 것입니다. 나는 내가 이미 가지고있는 코드에 난수 생성기를 통합하는 방법을 알아내는 데 도움이 필요하다고 생각합니다.

마지막으로 알고 싶은 것은 vb.net 또는 vb6입니까? 이것이 첫 번째 Visual Basic 프로젝트이므로이 차이를 실제로 알지 못합니다.

내가 잘못 알려주세요 그리고 난 전체 코드를 게시 할 수있는 경우 코드가 이동하지만 것입니다 가정 곳입니다 :

Public Class Form1 
Dim flag As Boolean = False 
Private mute As Integer = 0 

Private Sub Buttons(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click 

    If flag = False Then 
     ' DirectCast(sender, Button).Image = (My.Resources.Mario) 
     DirectCast(sender, Button).Text = "X" 
     flag = True 
     My.Computer.Audio.Play(My.Resources.fireball, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player O " 
     buttonDisable() 
     computerMove() 
    Else 
     ' DirectCast(sender, Button).Image = (My.Resources.Luigi) 
     DirectCast(sender, Button).Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If muteOff.Visible = True Then 
     My.Computer.Audio.Stop() 
    End If 
    win() 
    checkDraw() 
End Sub 

를이 내가 A "SMART"를 MAKE 쓴 아래의 코드입니다 컴퓨터 이동 나는 그것이 향상 될 수 있다고 생각하지만 나는 잘 모릅니다. 참고 : 전체 코드가 필요하지는 않지만 내가하는 일에 대한 힌트를 얻을 수 있다고 확신합니다.

Private Sub computerMove() 
    'XXO 
    If Button1.Text = "X" And Button2.Text = "X" And Button3.Enabled = True Then 
     Button3.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button2.Text = "X" And Button3.Text = "X" And Button1.Enabled = True Then 
     Button1.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button4.Text = "X" And Button5.Text = "X" And Button6.Enabled = True Then 
     Button6.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button6.Text = "X" And Button5.Text = "X" And Button4.Enabled = True Then 
     Button4.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button7.Text = "X" And Button8.Text = "X" And Button9.Enabled = True Then 
     Button9.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button9.Text = "X" And Button8.Text = "X" And Button7.Enabled = True Then 
     Button7.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button1.Text = "X" And Button4.Text = "X" And Button7.Enabled = True Then 
     Button7.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button7.Text = "X" And Button4.Text = "X" And Button1.Enabled = True Then 
     Button1.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button2.Text = "X" And Button5.Text = "X" And Button8.Enabled = True Then 
     Button8.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button8.Text = "X" And Button5.Text = "X" And Button2.Enabled = True Then 
     Button2.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button3.Text = "X" And Button6.Text = "X" And Button9.Enabled = True Then 
     Button9.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button9.Text = "X" And Button6.Text = "X" And Button3.Enabled = True Then 
     Button3.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button1.Text = "X" And Button5.Text = "X" And Button9.Enabled = True Then 
     Button9.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button9.Text = "X" And Button5.Text = "X" And Button1.Enabled = True Then 
     Button1.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button3.Text = "X" And Button5.Text = "X" And Button7.Enabled = True Then 
     Button7.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button7.Text = "X" And Button5.Text = "X" And Button3.Enabled = True Then 
     Button3.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    'XOX 
    If Button1.Text = "X" And Button7.Text = "X" And Button4.Enabled = True Then 
     Button4.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button1.Text = "X" And Button3.Text = "X" And Button2.Enabled = True Then 
     Button2.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
    If Button1.Text = "X" And Button9.Text = "X" And Button5.Enabled = True Then 
     Button5.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button4.Text = "X" And Button6.Text = "X" And Button5.Enabled = True Then 
     Button5.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button2.Text = "X" And Button8.Text = "X" And Button5.Enabled = True Then 
     Button5.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button3.Text = "X" And Button9.Text = "X" And Button6.Enabled = True Then 
     Button6.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button3.Text = "X" And Button7.Text = "X" And Button5.Enabled = True Then 
     Button5.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button7.Text = "X" And Button9.Text = "X" And Button8.Enabled = True Then 
     Button8.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 

    If Button1.Text = "X" And Button3.Text = "X" And Button2.Enabled = True Then 
     Button2.Text = "O" 
     flag = False 
     My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background) 
     playerTurn.Text = " Turn: Player X " 
     buttonDisable() 
    End If 
End Sub 

마지막으로 한 가지 문제가 있습니다. 이 부분뿐만 아니라 코드의 다른 영역에서도 기능을 만들고 싶습니다. (나는 방금 말하기 때문에 용어에 익숙하지 않다) 버튼을 누른다. 버튼을 클릭하면 이벤트를 활성화하고 코드의 다른 위치에서 이벤트를 비활성화 할 수있는 버튼을 만듭니다. 예 : 시작 화면에 있고 1 명 또는 2 명의 플레이어를 재생하고 싶은지 묻는 단일 플레이어 단추에서 computerMove() 하위를 사용 가능하게 설정하고 다른 곳에서 코드를 사용하지 않거나 변경하십시오.

+0

VB.NET – jessehouwing

+0

과 매우 흡사합니다. 내가 원하는대로 정확하게 이해하려면 tic tac toe AI가 필요합니다. 여기 좀 봐 : http://stackoverflow.com/questions/15753572/simple-tic-tac-toe-ai 이것은 자바 스크립트지만, VB에서 적응하기가 어렵지 않아야한다. – tigrou

+0

나는 그것을 이해하는지 잘 모르겠습니다. 그것이 VB에 있었을지라도 그것을 얻으려면 잠깐 걸릴 수도 있습니다;) 감사합니다 네! –

답변

0

임의의 컴퓨터 이동에 대한 첫 번째 질문에 대답하십시오 : 9 개의 버튼을 가지고 있기 때문에 1과 9 사이의 임의의 정수를 반환하는 함수를 만들면됩니다. 나는 최근에 비슷한 것을해야했고 나의 결과는 이렇게 보였다.

Dim rnd As New System.Random 
Private Function generateRandomInt(ByVal min As Integer, ByVal max As Integer) 
    Dim value As Integer = rnd.Next(min, max + 1) 
    Return value 
End Function 

은 그럼 그냥

generateRandomInt(1, 9) 

당신은 더 배경 정보는 Random class을 확인할 수 있습니다 호출합니다.

그리고 약 CPU 동작 : 나는 게임 개발자는 아니지만 귀하의 접근 방식은 조금 복잡한 것 같습니다. 가능한 시나리오마다 if-case를 작성하는 대신 필드 (또는 케이스의 버튼)와 주변 필드를 동적으로 검사하여 사람 플레이어가 행 3에 근접한 지 여부를 결정하는 메소드를 작성해야합니다. 비록 복사 할 수있는 많은 예제와 튜토리얼이 있지만 확실합니다.