Write a program that will play tic-tac-toe [worth 50 pts] You will submit a SEPARATE R script.
1) drawing the board
draw.board <- function(board) { # Draw the board
xo <- c("X", " ", "O") # Symbols
par(mar = rep(0,4))
plot.new()
plot.window(xlim = c(0,30), ylim = c(0,30))
abline(h = c(10, 20), col="darkgrey", lwd = 4)
abline(v = c(10, 20), col="darkgrey", lwd = 4)
pieces <- xo[board + 2]
text(rep(c(5, 15, 25), 3), c(rep(25, 3), rep(15,3), rep(5, 3)), pieces, cex = 6)
# Identify location of any three in a row
square <- t(matrix(board, nrow = 3))
hor <- abs(rowSums(square))
if (any(hor == 3))
hor <- (4 - which(hor == 3)) * 10 - 5
else
hor <- 0
ver <- abs(colSums(square))
if (any(ver == 3))
ver <- which(ver == 3) * 10 - 5
else
ver <- 0
diag1 <- sum(diag(square))
diag2 <- sum(diag(t(apply(square, 2, rev))))
# Draw winning lines
if (hor > 0) lines(c(0, 30), rep(hor, 2), lwd=10, col="red")
if (ver > 0) lines(rep(ver, 2), c(0, 30), lwd=10, col="red")
if (abs(diag1) == 3) lines(c(2, 28), c(28, 2), lwd=10, col="red")
if (abs(diag2) == 3) lines(c(2, 28), c(2, 28), lwd=10, col="red")
}
2) Random Tic Tac Toe
eval.winner <- function(board) { # Identify winner
square <- t(matrix(board, nrow = 3))
hor <- rowSums(square)
ver <- colSums(square)
diag1 <- sum(diag(square))
diag2 <- sum(diag(t(apply(square, 2, rev))))
if (3 %in% c(hor, ver, diag1, diag2)) return (1)
else
if (-3 %in% c(hor, ver, diag1, diag2)) return (2)
else
return(0)
}
# Random game
library(animation)
saveGIF ({
for (i in 1:10) {
game <- rep(0, 9) # Empty board
winner <- 0 # Define winner
player <- -1 # First player
draw.board(game)
while (0 %in% game & winner == 0) { # Keep playing until win or full board
empty <- which(game == 0) # Define empty squares
move <- empty[sample(length(empty), 1)] # Random move
game[move] <- player # Change board
draw.board(game)
winner <- eval.winner(game) # Evaulate game
player <- player * -1 # Change player
}
draw.board(game)
}
},
interval = 0.25, movie.name = "ttt.gif", ani.width = 600, ani.height = 600)
Write a program that will play tic-tac-toe [worth 50 pts] You will submit a SEPARATE R...
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
Write a JavaFX application program to play an interactive version of Tic-Tac-Toe
1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has...
In Java create a program where a user plays Tic Tac Toe against the computer. Create a class TicTacToe that will enable you write a program to play Tic-Tac-Toe. The class contains a private 3x3 two-dimensional array. Use an enum type to represent the value in each cell of the array. The enum’s constants should be named X, O, and EMPTY (for a position that does not contain an X or O). The constructor should initialize the board elements to...
How to write a (c++ program) tic tac toe code where its the computer against a human. and the computer always wins. a basic c++ code program using gaming theory arrays <stdio.h> computer plays tic tac toe against a user. computer always when or the game is a draw.
Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...