Lynix_

X O skeleton code

Dec 11th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.76 KB | None | 0 0
  1. Module Module1
  2.     'Skeleton Program code for the AQA COMP1 Summer 2010 examination
  3.     'this code should be used in conjunction with the Preliminary Materials
  4.     'written by the AQA COMP1 Programmer Team
  5.     'developed in the Visual Studio 2008 (Console Mode) programming environment (VB.NET)
  6.  
  7.     Dim Board(3, 3) As Char
  8.     Dim PlayerOneName As String
  9.     Dim PlayerTwoName As String
  10.     Dim PlayerOneScore As Single
  11.     Dim PlayerTwoScore As Single
  12.     Dim XCoord As Integer
  13.     Dim YCoord As Integer
  14.     Dim NoOfMoves As Integer
  15.     Dim ValidMove As Boolean
  16.     Dim GameHasBeenWon As Boolean
  17.     Dim GameHasBeenDrawn As Boolean
  18.     Dim CurrentSymbol As Char
  19.     Dim StartSymbol As Char
  20.     Dim PlayerOneSymbol As Char
  21.     Dim PlayerTwoSymbol As Char
  22.     Dim Answer As Char
  23.  
  24.     Sub Main()
  25.         Randomize()
  26.         Console.Write("What is the name of player one? ")
  27.         PlayerOneName = Console.ReadLine()
  28.         Console.Write("What is the name of player two? ")
  29.         PlayerTwoName = Console.ReadLine()
  30.         Console.WriteLine()
  31.         PlayerOneScore = 0
  32.         PlayerTwoScore = 0
  33.         Do 'Choose player one’s symbol
  34.             Console.Write(PlayerOneName & " what symbol do you wish to use, X or O? ")
  35.             PlayerOneSymbol = Console.ReadLine()
  36.             Console.WriteLine()
  37.             If Not (PlayerOneSymbol = "X" Or PlayerOneSymbol = "O") Then
  38.                 Console.WriteLine("Symbol to play must be uppercase X or O")
  39.                 Console.WriteLine()
  40.             End If
  41.         Loop Until PlayerOneSymbol = "X" Or PlayerOneSymbol = "O"
  42.         If PlayerOneSymbol = "X" Then
  43.             PlayerTwoSymbol = "O"
  44.         Else
  45.             PlayerTwoSymbol = "X"
  46.         End If
  47.         StartSymbol = GetWhoStarts()
  48.         Do 'Play a game
  49.             NoOfMoves = 0
  50.             GameHasBeenDrawn = False
  51.             GameHasBeenWon = False
  52.             ClearBoard(Board)
  53.             Console.WriteLine()
  54.             DisplayBoard(Board)
  55.             If StartSymbol = PlayerOneSymbol Then
  56.                 Console.WriteLine(PlayerOneName & " starts playing " & StartSymbol)
  57.             Else
  58.                 Console.WriteLine(PlayerTwoName & " starts playing " & StartSymbol)
  59.             End If
  60.             Console.WriteLine()
  61.             CurrentSymbol = StartSymbol
  62.             Do 'Play until a player wins or the game is drawn
  63.                 Do 'Get a valid move
  64.                     GetMoveCoordinates(XCoord, YCoord)
  65.                     ValidMove = CheckValidMove(XCoord, YCoord, Board)
  66.                     If Not ValidMove Then Console.WriteLine("Coordinates invalid, please try again")
  67.                 Loop Until ValidMove
  68.                 Board(XCoord, YCoord) = CurrentSymbol
  69.                 DisplayBoard(Board)
  70.                 GameHasBeenWon = CheckXOrOHasWon(Board)
  71.                 NoOfMoves = NoOfMoves + 1
  72.                 If Not GameHasBeenWon Then
  73.                     If NoOfMoves = 9 Then 'Check if maximum number of allowed moves has been reached
  74.                         GameHasBeenDrawn = True
  75.                     Else
  76.                         If CurrentSymbol = "X" Then
  77.                             CurrentSymbol = "O"
  78.                         Else
  79.                             CurrentSymbol = "X"
  80.                         End If
  81.                     End If
  82.                 End If
  83.             Loop Until GameHasBeenWon Or GameHasBeenDrawn
  84.             If GameHasBeenWon Then ' Update scores and display result
  85.                 If PlayerOneSymbol = CurrentSymbol Then
  86.                     Console.WriteLine(PlayerOneName & " congratulations you win!")
  87.                     PlayerOneScore = PlayerOneScore + 1
  88.                 Else
  89.                     Console.WriteLine(PlayerTwoName & " congratulations you win!")
  90.                     PlayerTwoScore = PlayerTwoScore + 1
  91.                 End If
  92.             Else
  93.                 Console.WriteLine("A draw this time!")
  94.             End If
  95.             Console.WriteLine()
  96.             Console.WriteLine(PlayerOneName & " your score is: " & PlayerOneScore)
  97.             Console.WriteLine(PlayerTwoName & " your score is: " & PlayerTwoScore)
  98.             Console.WriteLine()
  99.             If StartSymbol = PlayerOneSymbol Then
  100.                 StartSymbol = PlayerTwoSymbol
  101.             Else
  102.                 StartSymbol = PlayerOneSymbol
  103.             End If
  104.             Console.Write("Another game Y/N?")
  105.             Answer = Console.ReadLine()
  106.         Loop Until Answer = "N" Or Answer = "n"
  107.     End Sub
  108.  
  109.     Sub DisplayBoard(ByVal Board(,) As Char)
  110.         Dim Row As Integer
  111.         Dim Column As Integer
  112.         Console.WriteLine("  | 1 2 3 ")
  113.         Console.WriteLine("--+-------")
  114.         For Row = 1 To 3
  115.             Console.Write(Row & " | ")
  116.             For Column = 1 To 3
  117.                 Console.Write(Board(Column, Row) & " ")
  118.             Next
  119.             Console.WriteLine()
  120.         Next
  121.         Console.WriteLine()
  122.     End Sub
  123.  
  124.     Sub ClearBoard(ByRef Board(,) As Char)
  125.         Dim Row As Integer
  126.         Dim Column As Integer
  127.         For Row = 1 To 3
  128.             For Column = 1 To 3
  129.                 Board(Column, Row) = " "
  130.             Next
  131.         Next
  132.     End Sub
  133.  
  134.     Sub GetMoveCoordinates(ByRef XCoordinate As Integer, ByRef YCoordinate As Integer)
  135.         Console.Write("Enter x coordinate: ")
  136.         XCoordinate = Console.ReadLine()
  137.         Console.Write("Enter y coordinate: ")
  138.         YCoordinate = Console.ReadLine()
  139.         Console.WriteLine()
  140.     End Sub
  141.  
  142.     Function CheckValidMove(ByVal XCoordinate As Integer, ByVal YCoordinate As Integer, _
  143. ByVal Board(,) As Char)
  144.         Dim ValidMove As Boolean
  145.         ValidMove = True
  146.         'Check x coordinate is valid
  147.         If XCoordinate < 1 Or XCoordinate > 3 Then ValidMove = False
  148.         CheckValidMove = ValidMove
  149.     End Function
  150.  
  151.     Function CheckXOrOHasWon(ByVal Board(,) As Char) As Boolean
  152.         Dim Row As Integer
  153.         Dim Column As Integer
  154.         Dim XOrOHasWon As Boolean
  155.         XOrOHasWon = False
  156.         For Column = 1 To 3
  157.             If Board(Column, 1) = Board(Column, 2) And Board(Column, 2) = Board(Column, 3) _
  158.                                     And Board(Column, 2) <> " " Then XOrOHasWon = True
  159.         Next
  160.         For Row = 1 To 3
  161.             If Board(1, Row) = Board(2, Row) And Board(2, Row) = Board(3, Row) _
  162.                             And Board(2, Row) <> " " Then XOrOHasWon = True
  163.         Next
  164.         CheckXOrOHasWon = XOrOHasWon
  165.     End Function
  166.  
  167.     Function GetWhoStarts() As Char
  168.         Dim RandomNo As Integer
  169.         Dim WhoStarts As Char
  170.         RandomNo = Rnd() * 100
  171.         If RandomNo Mod 2 = 0 Then
  172.             WhoStarts = "X"
  173.         Else
  174.             WhoStarts = "O"
  175.         End If
  176.         GetWhoStarts = WhoStarts
  177.     End Function
  178. End Module
Add Comment
Please, Sign In to add comment