Cows and Bulls: A simple word guessing game

“Cows and Bulls” is a simple word guessing game. In the beginning, the player is told how many letters there are in the word to guess. This is indicated by the number of stars or asterisks displayed. Then the player guesses the word. If the guess is correct, the player is declared a winner and is then prompted to continue or end the game.

If not, the player is told how many hits or “bulls” he made and how many misses or “cows” he made. The player is also asked if he wants to give up.If not, he must continue playing. If he gives up, he will be told what the word was. He will then be prompted to continue or end the game.

One of the subroutines in this program retrieves a random word from a dictionary file which is essentially a text file containing one word per line. It stores all the words in a list and then picks a random element based on the size of the list.

This game has 2 versions: Java and C#

Java Version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cowsandbulls;
 
import java.io.*;
import java.util.*;
/**
 *
 * @author Dell
 */
public class CowsAndBulls {
 
    static ArrayList words = new ArrayList();
 
    private static String getRandWord(String path)
    {
        String randWord = "";
 
        try{
 
            InputStream istream = new FileInputStream("words.txt"); 
 
            InputStreamReader isr = new InputStreamReader(istream);
 
            BufferedReader br = new BufferedReader(isr);
 
            String word;
 
            while((word = br.readLine()) != null)
            {
                words.add(word);
            }
 
            int size = words.size();
 
            int idx = (new java.util.Random()).nextInt(size);
 
            randWord = words.get(idx);
 
        }
        catch(FileNotFoundException f)
        {
            System.out.println(f.getMessage());
 
            System.exit(0);
        }
        catch(IOException io)
        {
            System.out.println(io.getMessage());
 
            System.exit(0);
        }
 
        return randWord;
    }
 
    public static void main(String[] args)
    {
        char cont = '\0';
 
        Scanner sc  = new Scanner(System.in);
 
        System.out.println("WELCOME TO COWS AND BULLS!\n");
 
        do{
 
            String w = getRandWord("words.txt"); //ok
            //System.out.println(w);
 
            char[] guessChars = w.toCharArray();
 
            char giveUp = '\0';
 
 
 
            for(int i = 0; i< w.length(); i++)
            {
                System.out.print("* ");
            }
 
            System.out.println("\n");
 
            do
            {
 
                System.out.print("Guess the word: ");
 
                String guess = sc.nextLine();
 
                if(guess.equals(w))
                {
                    System.out.println("YOU WIN!\n");
 
                    break;
                }
                else
                {
 
                    if(guess.length() == w.length())
                    {
                        int cows = 0, bulls = 0;
 
                        for(int i = 0;i < w.length(); i++)
                        {
                            if(w.charAt(i) == guess.charAt(i))
                            {
                                bulls++;
 
                                System.out.print("bull ");
                            }
                            else
                            {
                                cows++;
 
                                System.out.print("cow ");
                            }
                        }
 
                        System.out.println();
 
                        System.out.println(bulls + " bulls, "+cows+" cows.\n");
                    }
                    else
                    {
                        System.out.println("LENGTH MISMATCH!");
                    }
 
 
                    System.out.println("Do you give up? (y/n): ");
 
                    String s = sc.nextLine();
 
                    while(s.length() == 0)
                    {
                        System.out.println("Do you give up? (y/n): ");
                        s = sc.nextLine();
                    }
 
                    giveUp = s.charAt(0);
 
                    //giveUp = sc.nextLine().charAt(0);
 
 
                }
            }
            while(giveUp != 'y' && giveUp != 'Y');
 
            if(giveUp == 'y' || giveUp == 'Y')
            {
                System.out.println("The word was: "+w);
            }
 
 
            System.out.println("Continue? (y/n): ");
 
            String s = sc.nextLine();
 
            while(s.length() == 0)
            {
                System.out.println("Continue? (y/n): ");
                s = sc.nextLine();
            }
 
            //
 
            cont = s.charAt(0);
 
 
        }while(cont == 'y' || cont == 'Y');
 
        System.out.println("Press any key to exit...");
 
        /* */
		System.console().readLine();
 
		System.exit(0);
 
    }
 
}

Copy, compile, run and ENJOY!

C# Version

The code is in the listing.
Also, see the video below.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace CowsAndBulls
{
    class Program
    {
 
        private static List lines = new List();
 
        private static string GetWordFromFile(string path)
        {
            System.IO.StreamReader file =
            new System.IO.StreamReader(@path);
 
            string line;
 
            while ((line = file.ReadLine()) != null)
            {
                lines.Add(line);
            }
 
            int count = lines.Count;
 
            Random r = new Random();
 
            int index = r.Next(count);
 
            string line1 = lines[index];
 
            file.Close();
 
            return line1; //working
        }
 
        static void Main(string[] args)
        {
            //
 
            char cont = '\0';
 
            Console.WriteLine("WELCOME TO COWS AND BULLS!\n");
 
            do
            {
 
                string w = GetWordFromFile("..\\..\\words.txt"); //ok
 
 
                char[] guessChars = w.ToCharArray();
 
                char giveUp = '\0';
 
                //
 
                for (int i = 0; i < w.Length; i++)
                {
                   Console.Write("* ");
                }
 
                Console.WriteLine("\n");
 
                do
                {
 
                    Console.Write("Guess the word: ");
 
                    string guess = Console.ReadLine();
 
                    if (guess == w)
                    {
                        Console.WriteLine("YOU WIN!\n");
 
                        break;
                    }
                    else
                    {
 
                        if (guess.Length == w.Length)
                        {
                            int cows = 0, bulls = 0;
 
                            for (int i = 0; i < w.Length; i++)
                            {
                                if (w.Substring(i, 1) == guess.Substring(i, 1))
                                {
                                    bulls++;
 
                                    Console.Write("bull ");
                                }
                                else
                                {
                                    cows++;
 
                                    Console.Write("cow ");
                                }
                            }
 
                            Console.WriteLine();
 
                            Console.WriteLine(bulls + " bulls, " + cows + " cows.\n");
                        }
                        else
                        {
                            Console.WriteLine("LENGTH MISMATCH!");
                        }
 
 
                        Console.WriteLine("Do you give up? (y/n): ");
 
                        string s = Console.ReadLine();
 
                        while (s.Length == 0)
                        {
                            Console.WriteLine("Do you give up? (y/n): ");
                            s = Console.ReadLine();
                        }
 
                        giveUp = Convert.ToChar(s.Substring(0, 1));
 
                        //
 
 
                    }
                }
                while (giveUp != 'y' && giveUp != 'Y');
 
                if (giveUp == 'y' || giveUp == 'Y')
                {
                    Console.WriteLine("The word was: " + w);
                }
 
 
                Console.WriteLine("Continue? (y/n): ");
 
                string s1 = Console.ReadLine();
 
                while (s1.Length == 0)
                {
                    Console.WriteLine("Continue? (y/n): ");
                    s1 = Console.ReadLine();
                }
 
                //
 
                cont = Convert.ToChar(s1.Substring(0, 1));
 
 
            } while (cont == 'y' || cont == 'Y');
 
            Console.WriteLine("Press any key to exit...");
 
            Console.Read();
 
        }
    }
}

 

Copy, compile, run and ENJOY!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *