Grzegorz Bernaś Blog Podróże i Programowanie to niekończąca się opowieść…

19paź/060

Zgaduj zgadula – program w C++

Prosta gra napisana na zaliczenie pierwszych ćwiczeń na Uczelni. Polega ona na wybraniu przedziału liczbowego a następnie zgadywaniu która liczba z podanego zakresu została wylosowana przez komputer. Program napisany w C++…

Download: ZgadujZgadula

zgaduj.cpp listing…

/*
 * Grzegorz Bernaś
 *
 * Written in Ubuntu Linux
 * Powered by Eclipse 3.1.2
 *
 * Copyright (C) 2006 Grzegorz Bernaś
 * Version 0.5
 * License http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * Zgadula is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

#include <iostream>
#include <ctime>

using namespace std;

#include "msg.h"
#include "zgadujlib.h"

//using namespace std;

int main()
{
	int zStart, zKoniec, zRand = 0, i = 0, zZakres, zInput, zRandMsg;

	cout << "\n \t Witam w programie Zgadula \n \n";
	cout << "Podaj zakres zgadywania - liczby całkowite\n";

	cout << "Wartość początkowa: ";
	cin >> zStart;

	cout << "Wartość końcowa: ";
	cin >> zKoniec;

	set_var(&zStart, &zKoniec);

	srand(time(NULL));

	zZakres = zStart - zKoniec + 1;
	zRand = (rand()%zZakres)+zKoniec;

	cout << "Jaką liczbę wylosował komputer z podanego zakresu?\n";

	while(zInput != zRand)
	{
		if ( i != 0 )
		{
			zRandMsg = rand()%5;
			cout << msg_to_usr(zRandMsg) <<"\n";
		}

		cout << "Próba nr"<< i+1 << ": ";
		cin >> zInput;

		i++;
	}

	cout << "\n \t Gratulację!\n";
	cout << "Udalo Ci się za " << i << " razem.\n";
	cout << "Właściwa liczba to: " << zInput << ".\n";

}

zgadujlib.h listing...

/*
 * Grzegorz Bernaś
 *
 * Written in Ubuntu Linux
 * Powered by Eclipse 3.1.2
 *
 * Copyright (C) 2006 Grzegorz Bernaś
 * Version 0.5
 * License http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * Zgadula is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

#ifndef _ZGADUJLIB_H_
#define _ZGADUJLIB_H_

void set_var(int *zStart, int *zKoniec)
{
	int tmp;

	//cout << *zStart << " " << *zKoniec << " \n";

	if ( *zKoniec > *zStart )
	{
		tmp = *zStart;
		*zStart = *zKoniec;
		*zKoniec = tmp;
	}

	cout << *zStart << " " << *zKoniec << " \n";
}

#endif //_ZGADUJLIB_H_

msg.h listing…

/*
 * Grzegorz Bernaś
 *
 * Written in Ubuntu Linux
 * Powered by Eclipse 3.1.2
 *
 * Copyright (C) 2006 Grzegorz Bernaś
 * Version 0.5
 * License http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * Zgadula is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

#ifndef _MSG_H_
#define _MSG_H_

const char* msg_to_usr( int num_tries )
{
      const int rsp_cnt = 5;
      static const char* usr_msgs[ rsp_cnt ] =
      {
        "Go on, make a guess. ",
        "Oops! Nice guess but not quite it.",
        "Hmm. Sorry. Wrong again.",
        "Ah, this is harder than it looks, no?",
        "It must be getting pretty frustrating by now!"
      };

      if ( num_tries < 0 )
         num_tries = 0;
      else
         if ( num_tries >= rsp_cnt )
              num_tries = rsp_cnt-1;
      return usr_msgs[ num_tries ];
}

#endif //_MSG_H_

Tags: , ,
Zakres tematyczny: C/C++, Programowanie Dodaj komentarz
Komentarze (0) Trackbacks (0)

Brak komentarzy.


Leave a comment

(required)


*

Brak trackbacków.