Interesting Console Program for C++ beginners - c++

I’m teaching an entry-level C++ programming class. We only use iostream in the class (No GUI). It seems like students are not so excited to printout strings and numbers to their console. (Most students even never used the console before.) It is hard to motivate or convey the excitement of programming by showing strings in their console.
What would be a good and exciting console program that can be written by C++ beginners? I’m looking for something doable with basic C++ skill + a bit challenging + very exciting, which can motivate students to learn programming languages.
Any comments will be appreciated.

When I taught an undergrad intro course, we did the Game of Fifteen in straight C as the third homework project. It's pretty well scoped, and it's a game, so there's some inherent motivation there.

Back when I taught, I made an early project be an ATM machine.
Text-only interface, with basic operations like withdraw, deposit, query balance, transfer between accounts, etc.
It was something that everyone was already familiar with, it didn't take huge amounts of programming time, but it did help students feel like it was a practical and realistic program.
Other similar ideas would be a cash-register (handle refunds, coupons, items priced by the pound, sales-tax, store specials, etc, etc), or a cell-phone billing program (separate daytime, night, and weekend minutes, bill text messages, picture messages separately, etc).

How about a system that generates a set of poker hands from a deck? While clearly defined, the intricacies of ensuring no duplicate cards etc, make it a good entry level challenge.
As an extension, you could have the system take an input as to whether you want to bet or fold, and effectively play a poker game.
Finally, a good design would allow them to switch the console for a gui front-end later on (e.g. intermediate class).

I always enjoyed problems where there's a real world purpose for doing it. Something like calculating a mathematical equation, or a range of prime numbers. A lot of stuff on ProjectEuler would be good, I would think. Not everybody likes math (but then again, it's kind of a necessary thing for computer science!).

Instead of just printing to the screen you could make ascii animations.

Introduce your students to pipes and filters. Create a useful utility that takes data from stdin and directs its output to stdout. Create another utility that does something else using that same protocol. Create a third utility. Demonstrate how robustly the utilities can work together.
For example, create a clone of the GNU head and tee utilities, and perhaps add a new utility called cap which capitalizes letters. Then demonstrate how you can get the first 3 lines of a text file capitalized and tee'd to a file and stdout. Next, demonstrate how you use the same utilities, without changing a single line of code, to take the first 5 lines of a file and output to the screen the capitalized letters and to a file the original letters.

When I took C++ we had to replicate a Theseus and the Minotaur game. It lends well to outputting multiple lines to the console to form something "graphical" and it's easily based on a set of implemented rules.

I've had to program a Tower of Hanoi game in the console before, I found that quite fun. It requires use of basic data structures, user input, checking for game end conditions so it would be good for a beginner I believe.

New programming students usually find graphical programs to be the most exciting.
It doesn't have to be anything really advanced, just being able to manipulate pixels and stuff should be enough to keep them interested. Making a simple graphic class around SDL should be ok. Maybe something like this:
int main()
{
GraphicWindow graphic;
graphic.setPixel(10,20,GraphicWindow::Red);
graphic.idle();
}
Then you give out assignments like "implement a drawRect function" etc.

Perhaps a text-only version of the Lunar Lander game. You could do full ASCII art and animations (with ncurses, perhaps) as an advanced exercise, but even in a pure text form it can be interesting.
I recall playing the version that ran on the HP 67 calculator, and it was entertaining with only a seven segment display to work with.
I vaguely recall a version that probably ran on an ALTAIR 8800 written in MITS/Microsoft BASIC that used the leading part of the line to show height above ground as ASCII art, with your prompt for the next tick's burn at the right.
Another traditional choice would be to implement Hunt the Wumpus, or for the ambitious, Battleship.

One of my first programming class had a long homework about implementing a (reduced) Monopoly game.
You can use chained lists for the board.
You can use Inheritance for board tiles.
You need some logic to process players turns.
It was probably the first project I've done in CS that I could talk about to my non-tech friends and generate some interest.

Related

Moving cursor using code and opening folder

This is a very random and maybe a bit strange question that i thought of at 3AM. I was thinking about how code could make my day to day life easier. Every morning I wake up, open chrome to the facebook conversations with my boyfriend, and write "good morning". And thats when i thought about this hypothetical project(just out of curiosity, I wouldn't use it haha): making a code that i can just run that does all of this for me.
I could have a html file that could redirect to the facebook link(https://www.facebook.com/messages/t/boyfriend_name). But how would I go on to make the code open this file, then move the mouse to where its supposed to go (the white area where the user inputs the text) then insert the text then press send?
I'm not asking for any code help as I can imagine that is too much, but my question is: could this be achievable in C++?(This is what we've been studying at school so far). If not, what coding language should I use? Is the idea achievable without a vast knowledge in computer science? If yes, have you got any sources about opening files using C++, moving cursor etc.
Note:The OS this would happen on is Windows 10
To do what you want is possible by using AutoIT and to use it from C++ you can try AutoITX for C++. With AutoIT it's possible to detect windows, move the mouse and insert text, although a web page is like a blackbox to it, so you'll have to rely on relative pixel coordinates (it might not be very robust).

How to read xml data as shown below in pandas dataframe?

I want to read the bold words as the column names in the dataframe and the string following the bold letters as the value for that particular row.
<posts>
<**row Id**="5" PostTypeId="1" **CreationDate**="2014-05-13T23:58:30.457" **Score**="7" ViewCount="315" **Body**="<p>I've always been interested in machine learning, but I can't figure out one thing about starting out with a simple "Hello World" example - how can I avoid hard-coding behavior?</p><p>For example, if I wanted to "teach" a bot how to avoid randomly placed obstacles, I couldn't just use relative motion, because the obstacles move around, but I don't want to hard code, say, distance, because that ruins the whole point of machine learning.</p><p>Obviously, randomly generating code would be impractical, so how could I do this?</p>" **OwnerUserId**="5" LastActivityDate="2014-05-14T00:36:31.077" Title="How can I do simple machine learning without hard-coding behavior?" Tags="<machine-learning>" AnswerCount="1" CommentCount="1" FavoriteCount="1" ClosedDate="2014-05-14T14:40:25.950"/>
<**row Id**="7" **PostTypeId**="1" **AcceptedAnswerId**="10" CreationDate="2014-05-14T00:11:06.457" Score="2" ViewCount="297" Body="<p>As a researcher and instructor, I'm looking for open-source books (or similar materials) that provide a relatively thorough overview of data science from an applied perspective. To be clear, I'm especially interested in a thorough overview that provides material suitable for a college-level course, not particular pieces or papers.</p>" OwnerUserId="36" LastEditorUserId="97" LastEditDate="2014-05-16T13:45:00.237"LastActivityDate="2014-05-16T13:45:00.237" Title="What open-source books (or other materials) provide a relatively thorough overview of data science?" Tags="<education><open-source>" AnswerCount="3" CommentCount="4" FavoriteCount="1" **ClosedDate**="2014-05-14T08:40:54.950"/>
</posts>

How to share data between two Lua scripts

I'm not even sure if my plan for doing things is the best way, so I apologize if this post is a bit vague. Also, I understand that similar questions have been asked before. However, I haven't been able to find anything that pertained to my situation and that made sense to me.
So me and my friends from school are building arcade machine, and I'm planning on putting together the main GUI that allows the user to select different games and load them if they have enough tokens. However, these separate windows will have to share some variables, mainly the number of tokens in the machine. I figured a separate Lua program could store such variable, and also have requests sent to it to perform other functions like opening and closing the different windows. Also, in case it's important to note, we will be using the Love2D engine for the games and be running all this on a Linux machine.
By what I've read, there seems to be some C and C++ code involved in this. I know next to nothing about C or C++, and we're trying to get this project moving along, so if you could include some code in your answer and instruct me on how to use it, that'd would be amazing. I can come back later and learn some C or C++, but right now Lua is my top priority.
My questions:
Is there a better way to accomplish what I'm trying to do?
How should I go about doing this?
Can this be done solely with Lua, or is some C, C++, or any other external programming language/utility/etc. required?
Also, incase anyone brings it up, I have tried using global variables, but I couldn't seem to get two programs/scripts to use the same variable at once.
Again, sorry if I'm being a bit vague.
Thanks in advance!
(this method is a combination of #Puzzlem00n's suggestion and reading the comments, so I shouldn't take much credit from this at all)
Putting multiple games in one lua program!
In main.lua:
require("spaceInvaders") --require all the other Lua files
-- without the ".lua" at the end!
gamestate=spaceInvaders
function love.load()
--all the setup things
end
function love.update()
gamestate.update()
end
function love.draw()
gamestate.draw()
end
function love.keypressed(key)
gamestate.keypressed(key)
end
--and so on for the rest of the love callbacks that you need (such as love.keyreleased())
Then in each game (which is a separate Lua file such as spaceInvaders.lua):
spaceInvaders = {}
function spaceInvaders.draw()
--your draw code
end
function spaceInvaders.update()
--your update code
end
--and so on for every other callback you want to use
What this code does is it gives each game its own set of love functions. When you want to play that game, you should set gamestate to that game's name. The spaceInvaders={} line defines spaceInvaders as a table, which stores each function in place. When you define a variable as an existing variable, you are actually creating a reference to it, for example:
t = {}
table.insert(t,1) --place 1 inside t
table.insert(t,3) --also place 3 inside t
s = t
print(s[1]) --will print 1, the first value of t
t[1]=2
print(s[1]) --will print 2, as it refers to t[1], which has just been changed
Sharing variables! (I worked out this bit!)
NOW, this means you can send variables around the program with another function. If you want to share score between games, you could do the following:
in main.lua:
function love.update()
gamestate.update()
score = gamestate.returnScore() --score will always equal to the current score being returned by the game
end
in a game, such as spaceInvaders.lua:
function spaceInvaders.returnScore()
return score --or whatever the score is stored in
end
This will allow you to get a variable, such as score, from one game to main.lua!
I'm sorry if this is a little confusing but hopefully this is what you're looking for! :)

How to get console text to refresh rather than re-type?

Hi so i'm making a game through the console window, and i was wondering if there was any way to just get maybe one or two text character's placement to change or disappear. Usually to accomplish this i would have to tell the console to re-type every single character and line all over again, but this just takes to long (1 second fps plus .5 second time spent re-typing the scene).
Is there some way i could re-fresh or change one or two lines or 'characters' seen on the console so so much time is not spent on waiting for the console to re-typing my 24 lines, each a string? (the scene made up of text)
Thanks! =)
btw... does anyone remember that little easter egg in windows which was an entire star wars movie made out of text in the console?? I want the game be smooth like that!
You'll need to use an external library to interface with the console as C++ doesn't have these capabilities, but it is possible.
My old goto for this sort of thing is ncurses. It's straightforward, quick to set up, and cross-platform. But it's old, and its age shows. (If you're on windows you'll have to use pdcurses; same capabilities, different package).
There are also console-specific ways of doing this. In particular, Windows provides an API for performing these sorts of actions.
You need ncurses library.
See console print w/o scrolling for reasons and examples.
Also google for the source to the rogue/urogue/nethack games which do that already.

Help with algorithm to dynamically update text display

First, some backstory:
I'm making what may amount to be a "roguelike" game so i can exersize some interesting ideas i've got floating around in my head. The gameplay isn't going to be a dungeon crawl, but in any case, the display is going to be done in a similar fasion, with simple ascii characters.
Being that this is a self exercise, I endeavor to code most of it myself.
Eventually I'd like to have the game runnable on arbitrarily large game worlds. (to the point where i envision havening the game networked and span over many monitors in a computer lab).
Right now, I've got some code that can read and write to arbitrary sections of a text console, and a simple partitioning system set up so that i can path-find efficiently.
And now the question:
I've ran some benchmarks, and the biggest bottleneck is the re-drawing of text consoles.
Having a game world that large will require an intelligent update of the display. I don't want to have to re-push my entire game buffer every frame... I need some pointers on how to set it up so that it only draws sections of the game have have been updated. (and not just individual characters as I've got now)
I've been manipulating the windows console via windows.h, but I would also be interested in getting it to run on linux machines over a puTTY client connected to the server.
I've tried adapting some video-processing routines, as there is nearly a 1:1 ratio between pixel and character, but I had no luck.
Really I want a simple explanation of some of the principles behind it. But some example (psudo)code would be nice too.
Use Curses, or if you need to be doing it yourself, read about the VTnnn control codes. Both of these should work on windows and on *nix terms and consoles (and Windows). You can also consult the nethack source code for hints. This will let you change characters on the screen wherever changes have happened.
I am not going to claim to understand this, but I believe this is close to the issue behind James Gosling's legendary Gosling Emacs redrawing code. See his paper, titled appropriately, "A Redisplay Algorithm", and also the general string-to-string correction problem.
Having a game world that large will
require an intelligent update of the
display. I don't want to have to
re-push my entire game buffer every
frame... I need some pointers on how
to set it up so that it only draws
sections of the game have have been
updated. (and not just individual
characters as I've got now)
The size of the game world isn't really relevant, as all you need to do is work out the visible area for each client and send that data. If you have a typical 80x25 console display then you're going to be sending just 2 or 3 kilobytes of data each time, even if you add in colour codes and the like. This is typical of most online games of this nature: update what the person can see, not everything in the world.
If you want to experiment with trying to find a way to cut down what you send, then feel free to do that for learning purposes, but we're about 10 years past the point where it is inefficient to update a console display in something approaching real time and it would be a shame to waste time fixing a problem that doesn't need fixing. Note that the PDF linked above gives an O(ND) solution whereas simply sending the entire console is half of O(N), where N is defined as the sum of the lengths of A and B and D.