Location: Minnesota, United States

A Blog on Minnesota and National politics, religion, news, and life.

Monday, November 08, 2004

blogAclass: Programming C++

Let's see what we are doing today in programming. First we are going through a small review of pointers in C++. For you non-programmers out here the simple definiton of pointers that I got from follows:
Pointers are variables that hold addresses in C and C++. They provide much power and utility for the programmer to access and manipulate data in ways not seen in some other languages. They are also useful for passing parameters into functions in a manner that allows a function to modify and return values to the calling routine. When used incorrectly, they also are a frequent source of both program bugs and programmer frustration.
Pointers, boring and not really the point of this specific lesson. The point of this lesson is to make a game of Tic-Tac-Toe usign C++. First we discuss how to play the game (whenever you are programming you need to start with the basics). For our game we first need to ask what row the player wants to put his symbol in, in our case we are labeling the rows 1,2, and 3 from top to bottom. Then we need to ask the same question of the columns on the tic-tac-toe board, labeling the columns 1,2, and 3 from left to right. Then we need to signify that it is the other players turn by outputting something like 'O's Turn'.
In programming you need to account for stupid people and thus every time something is input from the user we will need to have the computer check and make sure that the numbers 1,2, or 3 are used because obviously it is kinda hard to place a symbol somewhere off the grid. The game will continue like that, asking each player for the row and column until someone wins or there is a draw. So to sum up the last two paragaphs we have not even started programming, we are just setting up the game, covering the bases for each possible senario and planning, planning is very important in programming.
Five minutes later, still planning, we have drawn a Finite State Machine. That's kinda like a flowchart, ours looked like this, I drew mine in paint, isn't it pretty?

The Finite State Machine shows all the possible paths for each option as it comes up. We also came up with the following to check to see if a possible grid square is occuiped.
-Did X win?
-for each row r in the grid
-if piece at (r, x's column) is not x
-x did not win
-for each column c in the grid
-if piece at(x's row,c) is not x
-x did not win
-if piece was placed on the corners
-if piece at(1,0 0,2)or(2,3) or(2,1) or3,2) is not x than x did not win
The same bit of info translated into code for both X and O can also be used for O once a few things are changed in the ablve psudocode. The next bit is for checking to see if the board is full.
-Is the board full?
-for each rwo r
-for each column c
-if piece at(r,c) is blank board is not full
See, easy, right, lol. Probally really confusing but I hope you understand some of it. Class is about done and the prof has promised some coding on Wednesday. More on that then.