Scroll to see replies
procedure Multiplayer(BoardSize: integer);
var
Board: TBoard;
Player1Turn, MoveIsValid: boolean;
Move, Player1Score, Player2Score: integer;
Player1, Player2: string;
begin
Writeln('Player 1, What is your name?';
Readln(Player1);
Writeln('Player 2, What is your name?';
Readln(Player2);
P1 := Player1[1];
P2 := Player2[1];
SetUpGameBoard(Board, BoardSize);
Player1Turn := False;
repeat
Player1Turn := not Player1Turn;
DisplayGameBoard(Board, BoardSize);
MoveIsValid := False;
repeat
if Player1Turn then
Move := GetHumanPlayerMove(Player1)
else
Move := GetHumanPlayerMove(Player2);
MoveIsValid := CheckIfMoveIsValid(Board, Move);
until MoveIsValid;
MakeMove(Board, BoardSize, Move, Player1Turn);
until GameOver(Board, BoardSize);
DisplayGameBoard(Board, BoardSize);
Player1Score := GetPlayerScore(Board, BoardSize, P1);
Player2Score := GetPlayerScore(Board, BoardSize, P2);
if Player1Score > Player2Score then
Writeln('Well done, ', Player1, ', you have won the game!'
else
if Player1Score = Player2Score then
Writeln('That was a draw!'
else
Writeln('Well done, ', Player2, ', you have won the game!'
Writeln;
end;
#Skeleton Program for the AQA COMP1 Summer 2016 examination
#this code should be used in conjunction with the Preliminary Material
#written by the AQA COMP1 Programmer Team
#developed in a Python 3.4 programming environment
import random
def SetUpGameBoard(Board, Boardsize):
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if (Row == (BoardSize + 1) // 2 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2 + 1):
Board[Row][Column] = "C"
elif (Row == (BoardSize + 1) // 2 + 1 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2):
Board[Row][Column] = "H"
else:
Board[Row][Column] = " "
def SetUpMultiGameBoard(Board, Boardsize):
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if (Row == (BoardSize + 1) // 2 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2 + 1):
Board[Row][Column] = PlayerName1[0]
elif (Row == (BoardSize + 1) // 2 + 1 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2):
Board[Row][Column] = PlayerName2[0]
else:
Board[Row][Column] = " "
def ChangeBoardSize():
BoardSize = int(input("Enter a board size (between 4 and 9): "))
while not(BoardSize >= 4 and BoardSize <= 9):
BoardSize = int(input("Enter a board size (between 4 and 9): "))
return BoardSize
def GetHumanPlayerMove(PlayerName):
print(PlayerName, "enter the coodinates of the square where you want to place your piece: ", end="")
Coordinates = int(input())
return Coordinates
def GetComputerPlayerMove(BoardSize):
return random.randint(1, BoardSize) * 10 + random.randint(1, BoardSize)
def GameOver(Board, BoardSize):
for Row in range(1 , BoardSize + 1):
for Column in range(1, BoardSize + 1):
if Board[Row][Column] == " ":
return False
return True
def GetPlayersName():
PlayerName = input("What is your name? ")
return PlayerName
def GetPlayerName1():
PlayerName1 = input("What is your name? ")
return PlayerName1
def GetPlayerName2():
PlayerName2 = input("What is Player 2's name?")
return PlayerName2
def CheckIfMoveIsValid(Board, Move):
Row = Move % 10
Column = Move // 10
MoveIsValid = False
if Board[Row][Column] == " ":
MoveIsValid = True
return MoveIsValid
def GetPlayerScore(Board, BoardSize, Piece1, Piece2):
Score = 0
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if Board[Row][Column] == Piece1 or Piece2:
Score = Score + 1
return Score
def CheckIfThereArePiecesToFlip(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection):
RowCount = StartRow + RowDirection
ColumnCount = StartColumn + ColumnDirection
FlipStillPossible = True
FlipFound = False
OpponentPieceFound = False
while RowCount <= BoardSize and RowCount >= 1 and ColumnCount >= 1 and ColumnCount <= BoardSize and FlipStillPossible and not FlipFound:
if Board[RowCount][ColumnCount] == " ":
FlipStillPossible = False
elif Board[RowCount][ColumnCount] != Board[StartRow][StartColumn]:
OpponentPieceFound = True
elif Board[RowCount][ColumnCount] == Board[StartRow][StartColumn] and not OpponentPieceFound:
FlipStillPossible = False
else:
FlipFound = True
RowCount = RowCount + RowDirection
ColumnCount = ColumnCount + ColumnDirection
return FlipFound
def FlipOpponentPiecesInOneDirection(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection):
FlipFound = CheckIfThereArePiecesToFlip(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection)
if FlipFound:
RowCount = StartRow + RowDirection
ColumnCount = StartColumn + ColumnDirection
while Board[RowCount][ColumnCount] != " " and Board[RowCount][ColumnCount] != Board[StartRow][StartColumn]:
if Board[RowCount][ColumnCount] == "H":
Board[RowCount][ColumnCount] = "C"
else:
Board[RowCount][ColumnCount] = "H"
RowCount = RowCount + RowDirection
ColumnCount = ColumnCount + ColumnDirection
def MakeMove(Board, BoardSize, Move, HumanPlayersTurn):
Row = Move % 10
Column = Move // 10
if HumanPlayersTurn:
Board[Row][Column] = "H"
else:
Board[Row][Column] = "C"
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, -1)
def MakeMultiMove(Board, BoardSize, Move, HumanPlayersTurn):
Row = Move % 10
Column = Move // 10
if HumanPlayersTurn:
Board[Row][Column] = PlayerName1[0]
else:
Board[Row][Column] = PlayerName2[0]
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, -1)
def PrintLine(BoardSize):
print(" ", end="")
for Count in range(1, BoardSize * 2):
print("_", end="")
print()
def DisplayGameBoard(Board, BoardSize):
print()
print(" ", end="")
for Column in range(1, BoardSize + 1):
print(" ", end="")
print(Column, end="")
print()
PrintLine(BoardSize)
for Row in range(1, BoardSize + 1):
print(Row, end="")
print(" ", end="")
for Column in range(1, BoardSize + 1):
print("|", end="")
print(Board[Row][Column], end="")
print("|")
PrintLine(BoardSize)
print()
def DisplayMenu():
print("(p)lay game")
print("(e)nter name")
print("(c)hange board size")
print("(q)uit")
print("(m)ultiplayer mode")
print()
def GetMenuChoice(PlayerName):
print(PlayerName, "enter the letter of your chosen option: ", end="")
Choice = input()
return Choice
def CreateBoard():
Board = []
for Count in range(BoardSize + 1):
Board.append([])
for Count2 in range(BoardSize + 1):
Board[Count].append("")
return Board
def PlayGame(PlayerName, BoardSize):
Board = CreateBoard()
SetUpGameBoard(Board, BoardSize)
HumanPlayersTurn = False
while not GameOver(Board, BoardSize):
HumanPlayersTurn = not HumanPlayersTurn
DisplayGameBoard(Board, BoardSize)
MoveIsValid = False
while not MoveIsValid:
if HumanPlayersTurn:
Move = GetHumanPlayerMove(PlayerName)
else:
Move = GetComputerPlayerMove(BoardSize)
MoveIsValid = CheckIfMoveIsValid(Board, Move)
if not HumanPlayersTurn:
print("Press the Enter key and the computer will make its move")
input()
MakeMove(Board, BoardSize, Move, HumanPlayersTurn)
DisplayGameBoard(Board, BoardSize)
HumanPlayerScore = GetPlayerScore(Board, BoardSize, "H")
ComputerPlayerScore = GetPlayerScore(Board, BoardSize, "C")
if HumanPlayerScore > ComputerPlayerScore:
print("Well done", PlayerName, ", you have won the game!")
elif HumanPlayerScore == ComputerPlayerScore:
print("That was a draw!")
else:
print("The computer has won the game!")
print()
def PlayMultiGame(PlayerName1, PlayerName2, Boardsize):
Board = CreateBoard()
SetUpMultiGameBoard(Board, BoardSize)
HumanPlayersTurn = False
while not GameOver(Board, BoardSize):
HumanPlayersTurn = not HumanPlayersTurn
DisplayGameBoard(Board, BoardSize)
MoveIsValid = False
while not MoveIsValid:
if HumanPlayersTurn:
Move = GetHumanPlayerMove(PlayerName1)
else:
Move = GetHumanPlayerMove(PlayerName2)
HumanPlayersTurn = False
MoveIsValid = CheckIfMoveIsValid(Board, Move)
#if not HumanPlayersTurn:
#print("Press the Enter key for player 2's turn")
#input()
MakeMultiMove(Board, BoardSize, Move, HumanPlayersTurn)
DisplayGameBoard(Board, BoardSize)
HumanPlayer1Score = GetPlayerScore(Board, BoardSize, PlayerName1[0], "X")
HumanPlayer2Score = GetPlayerScore(Board, BoardSize, PlayerName2[0], "O")
if HumanPlayer1Score > HumanPlayer2Score:
print("Well done", PlayerName1, ", you have won the game!")
elif HumanPlayer1Score == HumanPlayer2Score:
print("That was a draw!")
else:
print("Well done", PlayerName2, ",you have won the game!")
print()
random.seed()
BoardSize = 6
PlayerName = ""
PlayerName1 = ""
Choice = ""
while Choice != "q":
DisplayMenu()
Choice = GetMenuChoice(PlayerName)
if Choice == "p":
PlayGame(PlayerName, BoardSize)
elif Choice == "e":
YU = (input("Is there a player2 Y/N? "))
while YU != "N" and YU != "Y":
YU = (input("Invalid, enter Y/N"))
if YU == "N":
PlayerName = GetPlayersName()
elif YU == "Y":
PlayerName1 = GetPlayerName1()
PlayerName2 = GetPlayerName2()
elif Choice == "c":
BoardSize = ChangeBoardSize()
elif Choice == "m":
if PlayerName1 == "":
PlayerName1 = "X"
PlayerName2 = "O"
PlayMultiGame(PlayerName1, PlayerName2, BoardSize)
else:
PlayMultiGame(PlayerName1, PlayerName2, BoardSize)
#Skeleton Program for the AQA COMP1 Summer 2016 examination
#this code should be used in conjunction with the Preliminary Material
#written by the AQA COMP1 Programmer Team
#developed in a Python 3.4 programming environment
import random
def SetUpGameBoard(Board, Boardsize):
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if (Row == (BoardSize + 1) // 2 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2 + 1):
Board[Row][Column] = "C"
elif (Row == (BoardSize + 1) // 2 + 1 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2):
Board[Row][Column] = "H"
else:
Board[Row][Column] = " "
def SetUpMultiGameBoard(Board, Boardsize):
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if (Row == (BoardSize + 1) // 2 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2 + 1):
Board[Row][Column] = PlayerName1[0]
elif (Row == (BoardSize + 1) // 2 + 1 and Column == (BoardSize + 1) // 2 + 1) or (Column == (BoardSize + 1) // 2 and Row == (BoardSize + 1) // 2):
Board[Row][Column] = PlayerName2[0]
else:
Board[Row][Column] = " "
def ChangeBoardSize():
BoardSize = int(input("Enter a board size (between 4 and 9): ")
while not(BoardSize >= 4 and BoardSize <= 9):
BoardSize = int(input("Enter a board size (between 4 and 9): ")
return BoardSize
def GetHumanPlayerMove(PlayerName):
print(PlayerName, "enter the coodinates of the square where you want to place your piece: ", end=""
Coordinates = int(input())
return Coordinates
def GetComputerPlayerMove(BoardSize):
return random.randint(1, BoardSize) * 10 + random.randint(1, BoardSize)
def GameOver(Board, BoardSize):
for Row in range(1 , BoardSize + 1):
for Column in range(1, BoardSize + 1):
if Board[Row][Column] == " ":
return False
return True
def GetPlayersName():
PlayerName = input("What is your name? "
return PlayerName
def GetPlayerName1():
PlayerName1 = input("What is your name? "
return PlayerName1
def GetPlayerName2():
PlayerName2 = input("What is Player 2's name?"
return PlayerName2
def CheckIfMoveIsValid(Board, Move):
Row = Move % 10
Column = Move // 10
MoveIsValid = False
if Board[Row][Column] == " ":
MoveIsValid = True
return MoveIsValid
def GetPlayerScore(Board, BoardSize, Piece1, Piece2):
Score = 0
for Row in range(1, BoardSize + 1):
for Column in range(1, BoardSize + 1):
if Board[Row][Column] == Piece1 or Piece2:
Score = Score + 1
return Score
def CheckIfThereArePiecesToFlip(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection):
RowCount = StartRow + RowDirection
ColumnCount = StartColumn + ColumnDirection
FlipStillPossible = True
FlipFound = False
OpponentPieceFound = False
while RowCount <= BoardSize and RowCount >= 1 and ColumnCount >= 1 and ColumnCount <= BoardSize and FlipStillPossible and not FlipFound:
if Board[RowCount][ColumnCount] == " ":
FlipStillPossible = False
elif Board[RowCount][ColumnCount] != Board[StartRow][StartColumn]:
OpponentPieceFound = True
elif Board[RowCount][ColumnCount] == Board[StartRow][StartColumn] and not OpponentPieceFound:
FlipStillPossible = False
else:
FlipFound = True
RowCount = RowCount + RowDirection
ColumnCount = ColumnCount + ColumnDirection
return FlipFound
def FlipOpponentPiecesInOneDirection(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection):
FlipFound = CheckIfThereArePiecesToFlip(Board, BoardSize, StartRow, StartColumn, RowDirection, ColumnDirection)
if FlipFound:
RowCount = StartRow + RowDirection
ColumnCount = StartColumn + ColumnDirection
while Board[RowCount][ColumnCount] != " " and Board[RowCount][ColumnCount] != Board[StartRow][StartColumn]:
if Board[RowCount][ColumnCount] == "H":
Board[RowCount][ColumnCount] = "C"
else:
Board[RowCount][ColumnCount] = "H"
RowCount = RowCount + RowDirection
ColumnCount = ColumnCount + ColumnDirection
def MakeMove(Board, BoardSize, Move, HumanPlayersTurn):
Row = Move % 10
Column = Move // 10
if HumanPlayersTurn:
Board[Row][Column] = "H"
else:
Board[Row][Column] = "C"
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, -1)
def MakeMultiMove(Board, BoardSize, Move, HumanPlayersTurn):
Row = Move % 10
Column = Move // 10
if HumanPlayersTurn:
Board[Row][Column] = PlayerName1[0]
else:
Board[Row][Column] = PlayerName2[0]
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, -1)
def PrintLine(BoardSize):
print(" ", end=""
for Count in range(1, BoardSize * 2):
print("_", end=""
print()
def DisplayGameBoard(Board, BoardSize):
print()
print(" ", end=""
for Column in range(1, BoardSize + 1):
print(" ", end=""
print(Column, end=""
print()
PrintLine(BoardSize)
for Row in range(1, BoardSize + 1):
print(Row, end=""
print(" ", end=""
for Column in range(1, BoardSize + 1):
print("|", end=""
print(Board[Row][Column], end=""
print("|"
PrintLine(BoardSize)
print()
def DisplayMenu():
print("(p)lay game"
print("(e)nter name"
print("(c)hange board size"
print("(q)uit"
print("(m)ultiplayer mode"
print()
def GetMenuChoice(PlayerName):
print(PlayerName, "enter the letter of your chosen option: ", end=""
Choice = input()
return Choice
def CreateBoard():
Board = []
for Count in range(BoardSize + 1):
Board.append([])
for Count2 in range(BoardSize + 1):
Board[Count].append(""
return Board
def PlayGame(PlayerName, BoardSize):
Board = CreateBoard()
SetUpGameBoard(Board, BoardSize)
HumanPlayersTurn = False
while not GameOver(Board, BoardSize):
HumanPlayersTurn = not HumanPlayersTurn
DisplayGameBoard(Board, BoardSize)
MoveIsValid = False
while not MoveIsValid:
if HumanPlayersTurn:
Move = GetHumanPlayerMove(PlayerName)
else:
Move = GetComputerPlayerMove(BoardSize)
MoveIsValid = CheckIfMoveIsValid(Board, Move)
if not HumanPlayersTurn:
print("Press the Enter key and the computer will make its move"
input()
MakeMove(Board, BoardSize, Move, HumanPlayersTurn)
DisplayGameBoard(Board, BoardSize)
HumanPlayerScore = GetPlayerScore(Board, BoardSize, "H"
ComputerPlayerScore = GetPlayerScore(Board, BoardSize, "C"
if HumanPlayerScore > ComputerPlayerScore:
print("Well done", PlayerName, ", you have won the game!"
elif HumanPlayerScore == ComputerPlayerScore:
print("That was a draw!"
else:
print("The computer has won the game!"
print()
def PlayMultiGame(PlayerName1, PlayerName2, Boardsize):
Board = CreateBoard()
SetUpMultiGameBoard(Board, BoardSize)
HumanPlayersTurn = False
while not GameOver(Board, BoardSize):
HumanPlayersTurn = not HumanPlayersTurn
DisplayGameBoard(Board, BoardSize)
MoveIsValid = False
while not MoveIsValid:
if HumanPlayersTurn:
Move = GetHumanPlayerMove(PlayerName1)
else:
Move = GetHumanPlayerMove(PlayerName2)
HumanPlayersTurn = False
MoveIsValid = CheckIfMoveIsValid(Board, Move)
#if not HumanPlayersTurn:
#print("Press the Enter key for player 2's turn"
#input()
MakeMultiMove(Board, BoardSize, Move, HumanPlayersTurn)
DisplayGameBoard(Board, BoardSize)
HumanPlayer1Score = GetPlayerScore(Board, BoardSize, PlayerName1[0], "X"
HumanPlayer2Score = GetPlayerScore(Board, BoardSize, PlayerName2[0], "O"
if HumanPlayer1Score > HumanPlayer2Score:
print("Well done", PlayerName1, ", you have won the game!"
elif HumanPlayer1Score == HumanPlayer2Score:
print("That was a draw!"
else:
print("Well done", PlayerName2, ",you have won the game!"
print()
random.seed()
BoardSize = 6
PlayerName = ""
PlayerName1 = ""
Choice = ""
while Choice != "q":
DisplayMenu()
Choice = GetMenuChoice(PlayerName)
if Choice == "p":
PlayGame(PlayerName, BoardSize)
elif Choice == "e":
YU = (input("Is there a player2 Y/N? ")
while YU != "N" and YU != "Y":
YU = (input("Invalid, enter Y/N")
if YU == "N":
PlayerName = GetPlayersName()
elif YU == "Y":
PlayerName1 = GetPlayerName1()
PlayerName2 = GetPlayerName2()
elif Choice == "c":
BoardSize = ChangeBoardSize()
elif Choice == "m":
if PlayerName1 == "":
PlayerName1 = "X"
PlayerName2 = "O"
PlayMultiGame(PlayerName1, PlayerName2, BoardSize)
else:
PlayMultiGame(PlayerName1, PlayerName2, BoardSize)
Last reply 1 day ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 3 days ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 weeks ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 1 month ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]Last reply 1 day ago
Is MathsFM French Alevels good for a CS degree at top uni (OX, UCL, Durham etc)Last reply 3 days ago
OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat]Last reply 2 weeks ago
OCR A-level Computer Science Paper 2 (H446/02) - 19th June 2023 [Exam Chat]Last reply 3 weeks ago
AQA A-level Computer Science Paper 1 (7517 1A - 1E) - 12th June 2023 [Exam Chat]Last reply 1 month ago
Not receiving any response from University of Birmingham.Last reply 3 months ago
OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat]Last reply 3 months ago
AQA GCSE Computer Science Paper 2 (8525/2) - 25th May 2023 [Exam Chat]