Language is Ada. Part 3 of 3 part question. Implement a function (or procedure i
ID: 3831749 • Letter: L
Question
Language is Ada. Part 3 of 3 part question. Implement a function (or procedure if you prefer) to determine if a player has won the game according to the standard rules of TicTacToe. Don't forget about diagonals. Modify the main program loop to stop either when there is a winner or when the board is full. Report the winner. with Ada.Text_IO; with Ada.Numerics.Discrete_Random; -- where we get the random generator procedure TTTLab is ------------------------------------------------------------------------ --| This program has the beginnings of a program for playing TicTacToe. --| Originally written by: Alan Garvey. --| Modified for this lab by: Your name here ------------------------------------------------------------------------ subtype Moverange is Natural range 1..3; -- range of TicTacToe moves type TTTValue is (X, O, Blank); -- possible board position values type TTTArray is array (MoveRange, MoveRange) of TTTValue; function IsFilled (Board : TTTArray) return Boolean is begin -- You need to fill in the real definition here. return false; end IsFilled; -- Instantiate a package for generating random values of type MoveRange package RandomMove is new Ada.Numerics.Discrete_Random (Result_Subtype => Moverange); -- Declare the generator to be used to generate random moves. Generator: Randommove.Generator; procedure MakeValidRandomMove (Board: in out TTTarray; Player: in TTTValue) is -- This procedure finds an empty board position and puts a piece from Player there. Row, Col: MoveRange; begin if Isfilled(Board => Board) then Ada.Text_IO.Put(Item=>"Error Error Error. Board is full."); else loop Row := Randommove.Random(Gen=> Generator); Col := Randommove.Random(Gen=> Generator); exit when Board(Row,Col) = Blank; end loop; end if; Board(Row,Col):= Player; end MakeValidRandomMove; procedure InitializeBoard (Board: in out TTTArray) is begin for row in MoveRange loop for col in MoveRange loop Board(row,col) := Blank; end loop; end loop; end InitializeBoard; procedure DisplayBoard (Board: in Tttarray) is begin -- You need to fill in the real definition here. null; end DisplayBoard; CurrentPlayer : TTTValue; TheBoard : TTTArray; begin -- TTTLab Ada.text_io.put_line(Item=>"Welcome to the TicTacToe Program."); -- Reset the random generator once at the beginning of the program. RandomMove.Reset(Gen=>Generator); CurrentPlayer := X; InitializeBoard(Board=> TheBoard); while not IsFilled(Board=> TheBoard) loop MakeValidRandomMove(Board=> TheBoard, Player => CurrentPlayer); DisplayBoard(Board=> TheBoard); if CurrentPlayer = X then CurrentPlayer := O; else CurrentPlayer := X; end if; end loop; Ada.Text_io.Put_Line(Item => "Thank you for using the program. Good-bye."); end TTTLab;
Explanation / Answer
Kindly find the below-working code for the same and also the output attached:
**************************
*********************
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.