Advertisement
Guest User

18

a guest
Oct 17th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #Problem 18
  2. winner = function(game)
  3. {
  4. # check winner by row
  5. for (row in 1:3)
  6. {
  7. un = unique(game[row,])
  8. if (length(un) == 1)
  9. return(un[1])
  10. }
  11. # check winner by column
  12. for (col in 1:3)
  13. {
  14. un = unique(game[,col])
  15. if (length(un) == 1)
  16. return(un[1])
  17. }
  18.  
  19. # check diagonal
  20. un = unique(c(game[1, 1], game[2, 2], game[3, 3]))
  21. if (length(un) == 1)
  22. return(un[1])
  23. return(NULL)
  24. }
  25.  
  26.  
  27. game = matrix(c("0", "+", "0",
  28. "+", "+", "+",
  29. "0", "0", "+"), nrow = 3)
  30. winner(game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement