Question

Write a program that will play tic-tac-toe [worth 50 pts] You will submit a SEPARATE R...

Write a program that will play tic-tac-toe [worth 50 pts] You will submit a SEPARATE R script.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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)

Add a comment
Know the answer?
Add Answer to:
Write a program that will play tic-tac-toe [worth 50 pts] You will submit a SEPARATE R...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (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...

  • 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

    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...

    (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...

    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 di...

    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...

    (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...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • tic-tac-toe game. Add functionality to the program so when the button is clicked for the AI...

    tic-tac-toe game. Add functionality to the program so when the button is clicked for the AI to take a turn, a heuristic is applied for each of the possible moves, a possible move is selected and the game state and GUI are properly updated. Once complete, the program should be able to play a single game of tic-tac-toe with the user. C#

  • JAVAFX PROGRAM Write a program that will allow two users to play a game of tic-tac-toe....

    JAVAFX PROGRAM Write a program that will allow two users to play a game of tic-tac-toe. The game area should consist of nine buttons for the play area, a reset button, and a label that will display the current player's turn. When the program starts, the game area buttons should have no values in them (i.e. they should be blank) When player one clicks on a game area button, the button should get the value X. When player two clicks...

  • Write a c program that will allow two users to play a tic-tac-toe game. You should...

    Write a c program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT