Hangman game with R

Hangman is a classic word game in which you need to need to guess as many possible letters in word, so you can guess the word, before running out of tries (lives).

Upon running out of tries, you are hanged!

hangman

The game can be played in R Studio, where the user inputs new letters in console, and the picture is being drawn (using library ggplot2). The picture consists of 7 false tries, so  it is drawn in 7 steps.

The diagram is created using simple X, Y coordinates with groups for determining the steps:

level1 <- data.frame(x=c(1,2,3,4,5,6,7,8), y=c(1,1,1,1,1,1,1,1), group=c(1,1,1,1,1,1,1,1))
level2 <- data.frame(x=c(4,4,4,4,4), y=c(1,2,3,4,5),group=c(2,2,2,2,2))
level3 <- data.frame(x=c(4,5,6), y=c(5,5,5), group=c(3,3,3))
level4 <- data.frame(x=c(6,6), y=c(5,4), group=c(4,4))
level5 <- drawHead(c(6,3.5),1,10,5)
level6 <- data.frame(x=c(6,6,5.8,6.2),y=c(3,1.5,1.5,1.5), group=c(6,6,6,6))
level7 <- data.frame(x=c(5.5,6,6.5),y=c(2,2.5,2), group=c(7,7,7))
levels <- rbind(level1,level2,level3,level4,level5,level6,level7)

Drawing itself is created by using a simple function using ggplot2 library:

drawMan <- function(st_napak) {
ggplot(levels[which(levels$group<=st_napak),], aes(x=x, y=y, group=group)) + 
geom_path(size=2.5) + 
theme_void()
}

The function draws the hanging man in 7 steps 🙂

All the rest of the logic is fairly simple, continue until you find the correct word, or until you are hanged. Section of the code:

beseda <- readline(prompt="Word: ")
iskana_beseda <- replicate(nchar(beseda),'_')

while (active == TRUE) {

if (i == 0) {
writeLines(paste(iskana_beseda, collapse = " "))
}
crka <- readline(prompt="Enter Letter: ")
izbor <- rbind(izbor, crka)

#iskana_beseda
if (grepl(crka, beseda) == TRUE) {

cilj <- rbind(cilj, crka)
iskana_beseda <- zamenjaj2(beseda, crka)
#print(zamenjaj2(beseda, crka)) 
print(paste("Yay!","Try N:",i+1,"Wrong letters: {",(toString(paste0(cilj_n, sep=","))),"}")) 

if (as.character(paste(iskana_beseda, collapse = "")) == beseda) {
active == FALSE
print("Bravo, win!")
break
}
{code continues.....}

… and the rest of there code is  here — >> github.

When playing, this is how it looks from my R Studio.

overView_R_studio

 

As always, complete code is available at Github.

Happy R-hanging 🙂

Tagged with: , , , , ,
Posted in Uncategorized
2 comments on “Hangman game with R
  1. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  2. […] November 19, 2019 By Donald Greer [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories
Follow TomazTsql on WordPress.com
Programs I Use
Programs I Use
Programs I Use
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

attaining enlightenment with sql server, .net, biztalk, windows and linux

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Ms SQL Girl

Julie Koesmarno's Journey In Data, BI and SQL World

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

SQL Server, Azure and DLM in a nutshell :D

Paul te Braak

Business Intelligence Blog

Sql Server Insane Asylum (A Blog by Pat Wright)

Information about SQL Server from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

%d bloggers like this: