Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Problem 18
- winner = function(game)
- {
- # check winner by row
- for (row in 1:3)
- {
- un = unique(game[row,])
- if (length(un) == 1)
- return(un[1])
- }
- # check winner by column
- for (col in 1:3)
- {
- un = unique(game[,col])
- if (length(un) == 1)
- return(un[1])
- }
- # check diagonal
- un = unique(c(game[1, 1], game[2, 2], game[3, 3]))
- if (length(un) == 1)
- return(un[1])
- return(NULL)
- }
- game = matrix(c("0", "+", "0",
- "+", "+", "+",
- "0", "0", "+"), nrow = 3)
- winner(game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement