Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:52:36+00:00 2026-06-08T21:52:36+00:00

I decided it was important for R users to be able to play hangman

  • 0

I decided it was important for R users to be able to play hangman and made an R hangman game. The problem is I don’t do many plots for general release and so I don’t know how to provide the user with a plot that works independent of platform or gui.

Here’s where you can download the complete package that contains large word bank:

library(devtools); install_github("hangman", "trinker")

The function that plots looks like this (I made the word bank go away for reproducibility):

hangman <- function() {
        #x1 <- DICTIONARY[sample(1:nrow(DICTIONARY), 1), 1]
        x1 <- "trapped"
        x <- unlist(strsplit(x1, NULL))
        len <- length(x)
        x2 <- rep("_", len)
        chance <- 0
        win1 <- 0
        win <- win1/len
        wrong <- character()
        right <- character()
        print(x2, quote = FALSE)
        hang.plot <- function(){ #plotting function
                plot.new()
                mtext("HANGMAN", col = "blue", cex=2)    
                mtext(paste(x2, collapse = " "), side = 1, cex=1.5) 
                mtext("wrong", side = 3, cex=1.5,
                          adj = 0, padj = 5, col = "red") 
                text(.015, .8, paste(wrong, collapse = "\n"), offset=.3, 
                         cex=1.5, adj=c(0,1))
                mtext("correct", side = 3, cex=1.5,
                          adj = 1, padj = 5, col = "red")
                text(.96, .8, paste(right, collapse = "\n"), offset=.3, 
                         cex=1.5, adj=c(0,1))
                segments(.365, .77, .365, .83, lwd=2)
                segments(.365, .83, .625, .83, lwd=2)
                segments(.625, .83, .625, .25, lwd=2)
                segments(.57, .25, .675, .25, lwd=2)
                parts <- seq_len(length(wrong))
                if (identical(wrong, character(0))) {
                        parts <- 0
                }
                if (1 %in% parts) {
                        mtext("O", side = 1, cex=4, adj = .365, padj = -7.2)
                        mtext("o o", side = 1, cex=1, adj = .3725, padj = -28.2)
                        mtext("<", side = 1, cex=1, adj = .373, padj = -27.6)
                        mtext("__", side = 1, cex=1, adj = .373, padj = -27.2)
                }
                if (2 %in% parts) {
                        mtext("I", side = 1, cex=4, adj = .375, padj = -6.25)
                        mtext("I", side = 1, cex=4, adj = .375, padj = -5.5)
                        mtext("I", side = 1, cex=4, adj = .375, padj = -4.75)
                }
                if (3 %in% parts) {
                        segments(.37, .57, .45, .63, lwd=7)
                }
                if (4 %in% parts) {
                        segments(.37, .57, .29, .63, lwd=7)
                }
                if (5 %in% parts) {
                        segments(.37, .426, .43, .3, lwd=7)
                        mtext("__", side = 1, cex = 1, adj = .373, 
                                  padj = -27.2, col = "white")
                        mtext("O", side = 1, cex = 1.25, adj = .373, padj = -21.5, 
                                  col="red")
                }
                if (6 %in% parts) {
                        segments(.37, .426, .31, .3, lwd = 7)
                        mtext("o o", side = 1, cex = 1, adj = .3725, 
                                  padj = -28.2, col="white")
                        mtext("x x", side = 1, cex=1, adj = .3725, padj = -28.2)
                        mtext("You Lose", side = 1, cex=8, padj = -3, 
                                  col = "darkgreen")
                        mtext(paste(x2, collapse = " "), side = 1, cex=1.6, col="white") 
                        mtext(paste(x2, collapse = " "), side = 1, cex=1.5, col="white") 
                        mtext(paste(x2, collapse = " "), side = 1, adj = .51, cex=1.6, 
                                  col="white")
                        mtext(paste(x, collapse = " "), side = 1, cex=1.5)
                }
                if (win1 == len) {
                        mtext("WINNER!", side = 1, cex=8, padj = -3, 
                                  col = "green")
                        mtext("WINNER!", side = 1, cex=8, adj = .1, padj = -3.1, 
                                  col = "darkgreen")
                }
        } #end of hang.plot
        guess <- function(){#start of guess function
                cat("\n","Choose a letter:","\n") 
                y <- scan(n=1,what = character(0),quiet=T)
                if (y %in% c(right, wrong)) {
                        stop(paste0("You've already guessed ", y))
                }
                if (!y %in% letters) {
                        stop(paste0(y, " is not a letter"))
                }
                if (y %in% x) {
                        right <<- c(right, y)
                        win1 <<- sum(win1, sum(x %in% y)) 
                        win <<- win1/len 
                        message(paste0("Correct!","\n"))
                } else {
                        wrong  <<- c(wrong, y)
                        chance  <<- length(wrong)
                        message(paste0("The word does not contain ", y, "\n"))
                }
                x2[x %in% right] <<- x[x %in% right]
                print(x2, quote = FALSE)
                hang.plot()
        }#end of guess function
        hang.plot()
        while(all(win1 != len & chance < 6)){ 
                try(guess())
        } 
        if (win == 1) {
                outcome <- "\nCongratulations! You Win!\n"
        } else {
                outcome <- paste("\nSorry. You loose. The word is:", x1, "\n")
        }
        cat(outcome)
}

This looks great on RGUI (in windows) where I created it but the plot is misconfigured in RStudio. How can I make the code plot everything in a way that lines up/looks good independent of gui/platform (my friend bryangoodrich suggested grid as a possibility)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:52:38+00:00Added an answer on June 8, 2026 at 9:52 pm

    Try to use text() instead of mtext(). All coordinate must be reconsider. Not tested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Decided to use Apache's Common Configuration package to parse an XML File. I decided
I decided to try to my hand at this, and have had a somewhat
I decided to have a go at PDO. The function below should move a
I decided to avoid for now the set preferences method for setting user settings.
I decided to take a different approach to mass assigning for security reasons and
I decided to learn how to use github about a month ago, and I
I decided not to use an orm and going to use straight ADO.NET for
I decided to learn Assembly language. The main reason to do so is being
I decided to give CodeRush/Refactor a go (after a rial of Resharper) and one
I decided to create a currency converter in Java, and have it so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.