library(tidyverse)
## ── Attaching packages ───────────────────────────────── tidyverse 1.3.0 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
setboard<-function(){
#Ranfomizing vertical and horizontal ship numbers
ships<-c(2,3,3,4,5)
ver_n<-sample(1:5,1)
ver_i<-sample(1:5,ver_n)
ver_s<-ships[ver_i]
hor_s<-ships[-ver_i]

# #Randomizing which rows and columns have ships
# hor_s_set<-sample(1:9,length(hor_s),rep=T)
# ver_s_set<-sample(1:11,length(ver_s),rep=T)

#Setting up gameboard
columns<-c("a","b","c","d","e","f","g","h","i","j","k")
rows<-c(0,0,0,0,0,0,0,0,0)
for(col in columns){
assign(col,rows)
}
a<-c(0,0,0,0,0,0,0,0,0)
board<-data.frame(a)%>%as_tibble()%>%cbind(b,c,d,e,f,g,h,i,j,k)
board_saved<-board
#Setting ships
drop<-11
iter<-1
while (drop==11){
set.seed(sample(1:10000,1))
board<-board_saved
print(paste("Starting iteration ",as.character(iter),sep=""))
count<-1

n_h<-length(hor_s)
n_v<-length(ver_s)

while(count<=length(hor_s)){
length<-hor_s[count]
start<-sample(1:(12-length),1)
end<-start+length-1
row<-sample(1:9,1)
while(sum(as.numeric(board[row,start:end]))>0){
  start<-sample(1:(12-length),1)
  end<-start+length-1
  row<-sample(1:9,1)
  }
board[row,start:(start+length-1)]<-count
count<-count+1
}

count_v<-1
drop<-1
while(count_v<=n_v){
length<-ver_s[count_v]
start<-sample(1:(10-length),1)
end<-start+length-1
collumn<-sample(1:11,1)
  while(sum(as.numeric(board[start:end,collumn]))>0){
  start<-sample(1:(10-length),1)
  collum<-sample(1:9,1)
  end<-start+length-1
  drop<-drop+1
  if(drop>10){
    break
  }
  }
board[start:end,collumn]<-count
count_v<-count_v+1
count<-count+1
iter<-iter+1
}
}
return(board)
}


new_track<-function(){
  columns<-c("a","b","c","d","e","f","g","h","i","j","k")
rows<-c(0,0,0,0,0,0,0,0,0)
for(col in columns){
assign(col,rows)
}
a<-c(0,0,0,0,0,0,0,0,0)
board<-data.frame(a)%>%as_tibble()%>%cbind(b,c,d,e,f,g,h,i,j,k)
return(board)
}

CPU<-setboard()
## [1] "Starting iteration 1"
User<-setboard()
## [1] "Starting iteration 1"
## [1] "Starting iteration 2"
CPU_track<-new_track()
User_track<-new_track()
end<-function(User=User,CPU_track=CPU_track){
  row<-sample(1:9,1)
  col<-sample(1:11,1)
  while(CPU_track[row,col]==9|is.na(CPU_track[row,col])){
    row<-sample(1:9,1)
    col<-sample(1:11,1)
  }
    if(User[row,col]==0){
      print("Computer missed")
      CPU_track[row,col]<-NA
    }else if(sum(as.numeric(User==User[row,col]))>1){
      print(paste("Computer has hit ship No.",as.character(User[row,col])))
      User[row,col]<-9
      CPU_track[row,col]<-9
    }else{
      print(paste("Computer has sunk ship No.",as.character(User[row,col])))
      User[row,col]<-9
      CPU_track[row,col]<-9
    }
  assign("User",User,envir=.GlobalEnv)
  assign('CPU_track',CPU_track,envir=.GlobalEnv)
    if(sum(as.numeric(User!=0),na.rm=T)==0){
    print("You have lost the  game of battleship")
    stop()
  }
  }






play<-function(row,col,CPU=CPU,User=User,User_track=User_track,CPU_track=CPU_track){
  if(is.na(User_track[row,col])|User_track[row,col]==9){
    print("You've tried this spot before, pick something else")
    stop()
  }else if(CPU[row,col]>0&&sum(as.numeric(CPU==CPU[row,col]))>1){
    print(paste("You have successfully hit ship No.",as.character(CPU[row,col])))
      User[row,col]<-9
      User_track[row,col]<-9
    }else if(CPU[row,col]==0) {
    print("You missed")
    User_track[row,col]<-NA
    }else{
      print(paste("You have sunk ship No.",as.character(User[row,col])))
      CPU[row,col]<-9
      User_track[row,col]<-9
    }
  assign("CPU",CPU,envir=.GlobalEnv)
  assign('User_track',User_track,envir=.GlobalEnv)
  if(sum(as.numeric(CPU!=0),na.rm=T)==0){
    print("Congrats! You've won battleship")
    stop()
  }
  end(User,CPU_track)
}

Tracking your missles

User_track%>%
  mutate_if(is.numeric, function(x) {
    cell_spec(x, bold = T, 
    color = spec_color(x, end = 0.9),
    font_size = spec_font_size(x))
    })%>%
  kable(row.names=TRUE,format='html',escape=F)%>%
  kable_styling(c("striped", "condensed"), full_width = F)
a b c d e f g h i j k
1 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0 0

Your Board

User%>%
  mutate_if(is.numeric, function(x) {
    cell_spec(x, bold = T, 
    color = spec_color(x, end = 0.9),
    font_size = spec_font_size(x))
    })%>%
  kable(row.names=TRUE,format='html',escape=F)%>%
  kable_styling(c("striped", "condensed"), full_width = F)
a b c d e f g h i j k
1 0 0 0 1 1 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0
3 0 0 4 4 4 4 3 3 3 0 0
4 0 0 2 2 2 0 0 0 0 0 0
5 5 0 0 0 0 0 0 0 0 0 0
6 5 0 0 0 0 0 0 0 0 0 0
7 5 0 0 0 0 0 0 0 0 0 0
8 5 0 0 0 0 0 0 0 0 0 0
9 5 0 0 0 0 0 0 0 0 0 0

Track your game