Ti-83 Programming Tutorials |
|||||
| |||||
Chapter 10 - MatricesIn the chapter 9, you learned how to use some of the list functions in programs. Matrices are 2-dimensional lists; that is, they contain elements stored in blocks of data in (row, column) format. Like chapter 9, we'll explore a program and showcase the implementation of matrices. Enjoy!Here is the Tic-Tac-Toe program you've been waiting for! Please note the "notes" in italics, as they should not be entered into the program. ClrHome DelVar [A] {3,3->dim([A] 1-A For(X,1,5 Output(X,1,sub(" : : -+-+-",6-10fPart(X/2),5 End Repeat 0 getKey->K If K Then For(X,0,2 For(Z,0,2 If (K=72+10X+Z)not([A](X+1,Z+1 Then A->[A](X+1,Z+1 Output(X2+1,Z2+1,sub("XO",A+3(A!=1),1 -A->A End End End abs(cumSum([A] For(X,0,1 FOr(Z,1,3 If 3=Ans(3,Z Goto W End abs(cumSum([A]t NOTE 't' means [MATRIX] MATH 2:t End NOTE: The following two lines are one statement. If (3=abs([A](1,1)+[A](2,2)+[A](3,3)) or (3=abs([A](1,3)+[A](2,2)+[A](3,1 Goto W 1 For(X,1,3 For(Z,1,3 Ans[A](X,Z End End If abs(Ans Goto Q End End Lbl Q Output(7,1,"TIE! Pause Stop Lbl W Output(7,1,"PLAYER "+sub("XO",-A+3(A=1),1)+" WINS! PauseA player hits the number pad to select the spot they wish to place their X or O on. Some new concepts are introduced in this program. First off, we use a list to dimension a matrix ({rows,columns}->[A] where rows is the number of rows, column is the number of columns, and [A] is the matrix). You can access a matrix element with [A](row,column). New functions introduced also include fPart(x) ("the fractional part of x") and the not(x) function (if x is 0, then the not returns a 1). Also, we've used cumSUm([A]) which returns a matrix with the cumulitave totals of the rows in a column down to the next row. The rest of the program, in fact, is nothing "new." It is rather the implementation that may seem new. There are only two variables declared (plus the temporary X and Z variables): A is the player currently selecting their spot; matrix [A] is the board, 3 rows by 3 columns. The beginning may seem intimidating at first, but really the first For loop just displays the board onto the screen. Of course, you could have used separte Output statements if you wanted to. Here's where size versus speed vs understandability versus functionality come into play (in fact, it comes up a lot in the program). It could be easier to just use separate Output statements, and in this instance speed and such isn't really a factor. What may concern you is if you understand the code. If you do, then good for you. If you don't, don't worry about it--try to at least visualize and understand the logic behind the code. The next line of code that may seem awkward is (K=72+10X+Z)not([A](X+1,Z+1. If you understand the logic, then it really isn't that difficult to understand. The keyboard code number for the number seven is 72, we add that to 10X (where X is from 0 to 2) and then add Z (from 0 to 2) to the number. This number is compared to K, the key the user has just pressed. The resulting number is either one of the 9 numbers of the keypad or not; Also, we must check to make sure that the the user wants to place their X/O on isn't already occupied. We do that with not([A](X+1,Z+1. If everything returns 1, then an X or O is placed on that spot and the matrix is updated. I have to say again, if you follow the logic closely, then the program becomes clearer. A player is either represented as -1 or 1, so A is inverted to get the other player. Because we have chosen the players to be represented as either -1 or 1, we can easily check if a person has three in a row (with the abs function). The 't' (transpose) function, found under [MATRIX] MATH 2:t switches a matrix's rows with its columns. Doing this, we can just loop twice to and transpose once to check for three in a row. The diagnals are checked separatedly. We also have to check to see if all the spaces are filled. We did this simply by multiplying all the values of the matrix together and if the answer wasn't 0 (that is, there was still a space open), then we know that all the spaces were filled. Other than that, the logic used in the program may be a little hard to grasp at first but if you follow program execution and the logic of the statements, especially the longer statements, then it will become easier to understand. More matrix (and list or other) fuctions can be looked up in the manual or other programs. The only way you're going to become familiar and comfortable with using these functions is if you implement them into your own program. Also, it really helps if you read the manual! You can download one in PDF format at education.ti.com. Also, if there are functions that you are unsure of, and you simply don't have the manual available, one of the best ways to understand or break down a function is to write test programs using these functions (or just use the home or graph display). For example, you may have used cumSum before (perhaps for math class) but did you ever think that it could be used in Tic-Tac-Toe program? It never hurts to understand the many functions of the calculator. Knowing how the calculator works may just be the one of the most important concepts next to logic and structuring of a program. |
|||||
|