i am programming tic tac toe in haskell. How can I check wether a player has a full row…
- User
i am programming tic tac toe in haskell. How can I check wether a player has a full row i.e. has won?
- Assistant
i am programming tic tac toe in haskell. How can I check wether a player has a full row i.e. has won?
Certainly! To check if a player has won in a game of Tic Tac Toe in Haskell, you need to verify if they have a complete row, column, or diagonal filled with their symbol. Let's consider a simple representation of the board as a 2D list (a list of lists).
Here's a step-by-step approach to check whether a player has won by filling a row:
Define the Board and Player Symbols: Assume the board is a 3x3 grid, and the players use 'X' and 'O' as their symbols.
Function to Check Rows: Create a function to check whether any row in the board is fully occupied by a given player's symbol.
Here's a basic implementation:
-- Define a type for the board
type Board = [[Char]]
-- Function to check if a list contains all the same elements
allEqual :: Eq a => [a] -> Bool
allEqual [] = False
allEqual (x:xs) = all (== x) xs
-- Function to check if a player has won by completing any row
hasFullRow :: Char -> Board -> Bool
hasFullRow playerSymbol board = any (all (== playerSymbol)) board
-- Example board
-- 'X' player wins with the top row filled
exampleBoard :: Board
exampleBoard = ["XXX",
"O O",
" XO"]
-- Testing the function
main :: IO ()
main = do
putStrLn $ if hasFullRow 'X' exampleBoard
then "Player X has a full row!"
else "Player X doesn't have a full row."
In this implementation:
allEqual function checks if all elements in a list are the same. If the list is empty, it returns False.
hasFullRow function checks each row of the board to see if all elements in the row are the player's symbol using the all (== playerSymbol) condition.
The exampleBoard is a sample board that you can use to test if the function works correctly.
When you run this code with main, it will output "Player X has a full row!" since the first row of exampleBoard is filled with 'X'.
You can extend this approach to also check columns and diagonals to fully determine if a player has won the game. Here is a quick extension with checks for columns and diagonals:
module TicTacToe (Board, hasWinner) where
type Board = [[Char]]
allEqual :: Eq a => [a] -> Bool
allEqual [] = False
allEqual (x:xs) = all (== x) xs
hasFullRow :: Char -> Board -> Bool
hasFullRow playerSymbol board = any (all (== playerSymbol)) board
hasFullColumn :: Char -> Board -> Bool
hasFullColumn playerSymbol board = any (all (== playerSymbol)) (transpose board)
hasFullDiagonal :: Char -> Board -> Bool
hasFullDiagonal playerSymbol board = all (== playerSymbol) (diagonal1 board) ||
all (== playerSymbol) (diagonal2 board)
where
diagonal1 b = [b !! i !! i | i <- [0..2]]
diagonal2 b = [b !! i !! (2 - i) | i <- [0..2]]
hasWinner :: Char -> Board -> Bool
hasWinner playerSymbol board = hasFullRow playerSymbol board ||
hasFullColumn playerSymbol board ||
hasFullDiagonal playerSymbol board
-- Example board
exampleBoard :: Board
exampleBoard = ["XXX",
"O O",
" XO"]
-- Testing the function
main :: IO ()
main = do
putStrLn $ if hasWinner 'X' exampleBoard
then "Player X has won!"
else "Player X has not won."
In this code, we added:
This setup will inform you if player 'X' or 'O' has won by completing a row, column, or diagonal on the Tic Tac Toe board.
Not yet included in a published release.