state <- as.character(1:9)winner <- FALSEtriples <- list( c(1,2,3), c(4,5,6), c(7,8,9), c(1,4,7), c(2,5,8), c(3,6,9), c(1,5,9), c(3,5,7))display<- function(state){ cat(" ", state1, "|", state2, "|", state3,"") cat(" ---+---+---", "") cat(" ", state4, "|", state5, "|", state6,"") cat(" ---+---+---", "") cat(" ", state7, "|", state8, "|", state9,"")}display(state)update<-function(state, who, pos){ if(who==1) {statepos<-"X"} else if(who==2) {statepos<-"O"} return(state)}play <- function(){ number_player<-readline("Type '1' to play against computer, Type '2' to play against player ") if(number_player==1){ who<-readline("Type '1' to play first or Type '2' to play second ") if(who == 1){ cat("Player Starts First !","") } else if(who == 2){ cat("Computer Starts First !","") } display(state) against_computer(state,who) } else if(number_player==2){ who<-1;cat("Player 1 Starts First !","") display(state) against_human(state,who) } else if(number_player !=1 && number_player !=2 ){ cat("Please type either 1 or 2") play() }}check_winner<-function(state){ for(i in 1:8){ if(sum(triplesi %in% which(state=="X")) >=3){ cat(“Player X wins”,””) winner = TRUE break } if(sum(triplesi %in% which(state==”O”)) >=3){ cat(“Player Y wins”,””) winner = TRUE break } } if(length(which(state==”X”))+length(which(state==”O”)) == 9 && winner==FALSE){ cat(“DRAW”, “”) winner=TRUE } ## Check whether there are any empty position and still no winner return(winner)}restriction<-function(state){ restrict<-TRUE while(restrict){ pos<-as.double(readline("Which position would you like to play? (Type a number from 1 to 9) :")) if((pos>=1 && pos<=9) != TRUE){ cat("Please type a number from 1 to 9","") } else{ if(statepos == "X" || statepos == "O"){ cat("Position is already occupied, please type the number with unoccupied position","") }else if(statepos != "X" && statepos != "O"){ state<-update(state,pos) restrict=FALSE } } } return(pos)}against_human<-function(state,who){ while(winner == FALSE){ pos<-restriction(state) if(who==1){ state<-update(state, who, pos) display(state) winner<-check_winner(state) who<-2 } else if(who==2){ state<-update(state, who, pos) display(state) winner<-check_winner(state) who<-1 } }}against_computer<-function(state,who){ while(winner == FALSE){ if(who==1){ pos<-restriction(state) state<-update(state, who, pos) display(state) winner<-check_winner(state) if(winner) break state<-computer_turn(state) cat("Computer makes this move: ", "") display(state) cat("Computer has taken the position",which(state=="O"), "") winner<-check_winner(state) if(winner) break } else if(who==2){ state<-computer_turn(state) cat("Computer makes this move: ", "") display(state) cat("Computer has taken the position",which(state=="X"), "") winner<-check_winner(state) if(winner) break pos<-restriction(state) state<-update(state, who, pos) display(state) winner<-check_winner(state) if(winner) break } }}computer_turn<-function(state){ finding_decider<-function(){ for(i in 1:8){ remainder =statetriplesiwhich((triplesi) %in% which(state=="X")== FALSE) which(remainder %in% as.character(1:9)) } } if(sum(state=="X") > sum(state==”O”)){#human goes first playing x and computer second playing o before<-state for(i in 1:8){ if(sum(triplesi %in% which(state=="X"))==2 && any(statetriplesiwhich((triplesi %in% which(state=="O"))==FALSE) %in% as.character(1:9)) && any(statetriplesiwhich((triplesi %in% which(state=="X"))==FALSE) %in% as.character(1:9))){ #winning statetriplesiwhich((triplesi %in% which(state=="O"))==FALSE)<-"O" } if(identical(state,before)==FALSE)break } if(identical(state,before)==TRUE){ for(i in 1:length(triples)){ if(sum(triplesi %in% which(state=="X"))==2 && any(statetriplesiwhich((triplesi %in% which(state=="X"))==FALSE) %in% as.character(1:9))&& any(statetriplesiwhich((triplesi %in% which(state=="O"))==FALSE) %in% as.character(1:9))){#blocking statetriplesiwhich((triplesi %in% which(state=="X"))==FALSE)<-"O" } if(identical(state,before)==FALSE)break } } if(identical(state,before)==TRUE){statesample(which(state == as.character(1:9)),1)<-"O"} }else if(sum(state=="O") >= sum(state==”X”) || sum(state==”O”)==0){#computer goes first playing x and human second playing o before<-state for(i in 1:length(triples)){ if(sum(triplesi %in% which(state=="X"))==2 && any(statetriplesiwhich((triplesi %in% which(state=="X"))==FALSE) %in% as.character(1:9))&& any(statetriplesiwhich((triplesi %in% which(state=="O"))==FALSE) %in% as.character(1:9))){#winning statetriplesiwhich((triplesi %in% which(state=="X"))==FALSE)<-"X" } if(identical(state,before)==FALSE) break } if(identical(state,before)==TRUE){ for(i in 1:length(triples)){ if(sum(triplesi %in% which(state=="O"))==2 && any(statetriplesiwhich((triplesi %in% which(state=="O"))==FALSE) %in% as.character(1:9))&& any(statetriplesiwhich((triplesi %in% which(state=="X"))==FALSE) %in% as.character(1:9))){#blocking statetriplesiwhich((triplesi %in% which(state=="O"))==FALSE)<-"X" } if(identical(state,before)==FALSE) break } } if(identical(state,before)==TRUE){statesample(which(state == as.character(1:9)),1)<-"X"} } return(state)} play()