Question

Write a C# windows forms program to create a States and Capitals guessing game. A method...

Write a C# windows forms program to create a States and Capitals guessing game. A method in your program should generate a random number between 1 and 50. Your program should use this number to represent one of the 50 U.S states and then display the name of that state. The user will guess the capital of that state. Another method in your program should get the users answer and check to see whether it is correct. Yet another method should print the players score, including both the total number of correct and incorrect guesses. Use classes, methods, arrays, to develop this program. Use one button to generate a random state to guess. Create another another button as a "cheat" button to see the answer without guessing.

String capitals [][] = {
        {"Alabama", "Montgomery"},
        {"Alaska", "Juneau"},
        {"Arizona", "Phoenix"},
        {"Arkansas", "Little Rock"},
        {"California", "Sacramento"},
        {"Colorado", "Denver"},
        {"Connecticut", "Hartford"},
        {"Delaware", "Dover"},
        {"Florida", "Tallahasse"},
        {"Georgia", "Atlanta"},
        {"Hawaii", "Honolulu"},
        {"Idaho", "Boise"},
        {"Illinois", "Springfield"},
        {"Indiana", "Indianapolis"},
        {"Iowa", "Des Moines"},
        {"Kansas", "Topeka"},
        {"Kentucky", "Frankfort"},
        {"Louisiana", "Baton Rouge"},
        {"Maine", "Augusta"},
        {"Maryland", "Annapolis"},
        {"Massachusettes", "Boston"},
        {"Michigan", "Lansing"},
        {"Minnesota", "Saint Paul"},
        {"Mississippi", "Jackson"},
        {"Missouri", "Jefferson City"},
        {"Montana", "Helena"},
        {"Nebraska", "Lincoln"},
        {"Nevada", "Carson City"},
        {"New Hampshire", "Concord"},
        {"New Jersey", "Trenton"},
        {"New York", "Albany"},
        {"New Mexico", "Santa Fe"},
        {"North Carolina", "Raleigh"},
        {"North Dakota", "Bismark"},
        {"Ohio", "Columbus"},
        {"Oklahoma", "Oklahoma City"},
        {"Oregon", "Salem"},
        {"Pennslyvania", "Harrisburg"},
        {"Rhode Island", "Providence"},
        {"South Carolina", "Columbia"},
        {"South Dakota", "Pierre"},
        {"Tennessee", "Nashville"},
        {"Texas", "Austin"},
        {"Utah", "Salt Lake City"},
        {"Vermont", "Montpelier"},
        {"Virginia", "Richmond"},
        {"Washington", "Olympia"},
        {"West Virginia", "Charleston"},
        {"Wisconsin", "Madison"},
        {"Wyoming", "Cheyenne"}
       };
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Download code. Open .sln file with visual studio. Run it.

Download code:

https://drive.google.com/open?id=1BHTWwWKCMWM71HLFG2VZsA9DWlqmizVG

Code:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CapitalGuessingGame
{
public partial class Form1 : Form
{
Data obj=new Data();
int positiveScore=0;
int negativeScore = 0;
public Form1()
{
InitializeComponent();
}

private void btnChangeState_Click(object sender, EventArgs e)
{
setNext();
}

private void setNext()
{
btnChangeState.Enabled = false;
lblState.Text=obj.getNextState();
btnChangeState.Enabled = false;
}

private void Form1_Load(object sender, EventArgs e)
{
setScore();
setNext();
}

public void setScore()
{
lblCorrect.Text = positiveScore.ToString();
lblIncorrect.Text = negativeScore.ToString();
}

private void button2_Click(object sender, EventArgs e)
{
String capital=obj.getCapital(lblState.Text);
String answer= tbCapital.Text;
if (answer.Equals(capital))
{
positiveScore++;
MessageBox.Show("Correct");
}
else
{
negativeScore++;
MessageBox.Show("Incorrect");
}
setScore();
btnChangeState.Enabled = true;
}

private void btnCheat_Click(object sender, EventArgs e)
{
String capital = obj.getCapital(lblState.Text);
tbCapital.Text = capital;
btnChangeState.Enabled = true;
}
}
}

Data.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CapitalGuessingGame
{
public class Data
{
private String[,] capitals;
Random r = new Random();
public Data()
{
capitals = new String[50, 2] {{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahasse"},
{"Georgia", "Atlanta"},
{"Hawaii", "Honolulu"},
{"Idaho", "Boise"},
{"Illinois", "Springfield"},
{"Indiana", "Indianapolis"},
{"Iowa", "Des Moines"},
{"Kansas", "Topeka"},
{"Kentucky", "Frankfort"},
{"Louisiana", "Baton Rouge"},
{"Maine", "Augusta"},
{"Maryland", "Annapolis"},
{"Massachusettes", "Boston"},
{"Michigan", "Lansing"},
{"Minnesota", "Saint Paul"},
{"Mississippi", "Jackson"},
{"Missouri", "Jefferson City"},
{"Montana", "Helena"},
{"Nebraska", "Lincoln"},
{"Nevada", "Carson City"},
{"New Hampshire", "Concord"},
{"New Jersey", "Trenton"},
{"New York", "Albany"},
{"New Mexico", "Santa Fe"},
{"North Carolina", "Raleigh"},
{"North Dakota", "Bismark"},
{"Ohio", "Columbus"},
{"Oklahoma", "Oklahoma City"},
{"Oregon", "Salem"},
{"Pennslyvania", "Harrisburg"},
{"Rhode Island", "Providence"},
{"South Carolina", "Columbia"},
{"South Dakota", "Pierre"},
{"Tennessee", "Nashville"},
{"Texas", "Austin"},
{"Utah", "Salt Lake City"},
{"Vermont", "Montpelier"},
{"Virginia", "Richmond"},
{"Washington", "Olympia"},
{"West Virginia", "Charleston"},
{"Wisconsin", "Madison"},
{"Wyoming", "Cheyenne"}
};
}
public String getNextState(){
int rInt = r.Next(0, 50);
return capitals[rInt, 0];
}

public String getCapital(String state)
{
for(int i=0;i<capitals.Length;i++){
if (capitals[i, 0].Equals(state))
{
return capitals[i, 1];
}
}
return null;
}

}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a C# windows forms program to create a States and Capitals guessing game. A method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA: File USCapitals.txt is here: Alabama, Montgomery Alaska, Juneau Arizona, Phoenix Arkansas, Little Rock California, Sacramento...

    JAVA: File USCapitals.txt is here: Alabama, Montgomery Alaska, Juneau Arizona, Phoenix Arkansas, Little Rock California, Sacramento Colorado, Denver Connecticut, Hartford Delaware, Dover Florida, Tallahassee Georgia, Atlanta Hawaii, Honolulu Idaho, Boise Illinois, Springfield Indiana, Indianapolis Iowa, Des Moines Kansas, Topeka Kentucky, Frankfort Louisiana, Baton Rouge Maine, Augusta Maryland, Annapolis Massachusettes, Boston Michigan, Lansing Minnesota, Saint Paul Mississippi, Jackson Missouri, Jefferson City Montana, Helena Nebraska, Lincoln Nevada, Carson City New Hampshire, Concord New Jersey, Trenton New York, Albany New Mexico, Santa Fe...

  • My project has to do with the average tuition for in-state students per semester and the proportion of western states th...

    My project has to do with the average tuition for in-state students per semester and the proportion of western states that pay less than $5000 for tuition. Western states include Alaska, Arizona, California, Colorado, Hawaii, Idaho, Montana, Nevada, New Mexico, Oregon, Utah, Washington and Wyoming. I need help with the best method of graphing this and how to create the confidence intervals for 95% for BOTH the average tuition for in state students AND the proportion of western states that...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

  • The accompanying table shows the average price for a gallon of gas for the 50 states...

    The accompanying table shows the average price for a gallon of gas for the 50 states during April 2012. State Price per Gallon Alaska 4.36 Alabama 3.79 Arkansas 3.76 Arizona 3.87 California 4.22 Colorado 3.89 Connecticut 4.17 Delaware 3.89 Florida 3.94 Georgia 3.83 Hawaii 4.61 Iowa 3.75 Idaho 3.77 Illinois 4.06 Indiana 3.95 Kansas 3.71 Kentucky 3.92 Louisiana 3.80 Massachusetts 3.91 Maryland 3.96 Maine 4.00 Michigan 3.93 Minnesota 3.72 Missouri 3.67 Mississippi 3.78 Montana 3.77 North Carolina 3.89 North Dakota...

  • Create a Wpf application Use Visual Studios 2019 A shipping company receives packages at its headquarters,...

    Create a Wpf application Use Visual Studios 2019 A shipping company receives packages at its headquarters, which functions as its shipping hub. After receiving the packages the company ships them to a distribution center in one of the following states: Alabama, Florida, Georgia, Kentucky, Mississippi, North Carolina, South Carolina, Tennessee, West Virginia or Virginia. The company needs an application to track the packages that pass through its shipping hub. The application generates a package ID number for each package that...

  • I am trying to do this assignment: Write a program that reads a list of concepts/strings...

    I am trying to do this assignment: Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line), stores them into an array of strings, and then prints the following: 1.Prints a heading “LIST1:” and then prints all the concepts from the array, one concept per line 2.Prints a heading “LIST2:” and then prints all the concepts from the array that do not contain spaces, one concept per line 3.Prints a heading “LIST3:”...

  • 3.26 c. Based on the histograms from Parts (a) and (b). write a few sentences commenting...

    3.26 c. Based on the histograms from Parts (a) and (b). write a few sentences commenting on the simi- larities and differences in the distribution of SAT critical reading scores formales and females Relative Frequency for Females 43.4 Country Australia Austria Canada Colombia Costa Rica Czech Republic Denmark Finland France Germany Greece Hungary Iceland Israel Percentage of people age 25 to 34 with 4.sear degree 49.3 39.7 60.6 28.1 28.9 32.6 45.9 41.1 44.0 30.5 41.0 30.4 43.3 47.4 Percentage...

  • JAVA Create a Governor class with the following attributes: name : String party - char (the...

    JAVA Create a Governor class with the following attributes: name : String party - char (the character will be either D, R or I) ageWhenElected : int Create a State class with the following attributes: name : String abbreviation : String population : long governor : Governor Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy). You will be turning...

  • C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The...

    C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...

  • Form Processing HTML One of the most ubiquitous uses of JavaScript is validating form data on...

    Form Processing HTML One of the most ubiquitous uses of JavaScript is validating form data on the client side before it is submitted to the server. It is done everywhere because it is fast and it gives you a great deal of flexibility in how you handle errors insofar as the GUI is concerned. Attached is an image of some code I wrote (so Blackboard can't mess it up). Some things to notice that will help you with the lab....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT