259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

Slides:



Advertisements
Similar presentations
Lab 8 User Defined Function.
Advertisements

CHAPTER 10 FUN AND GAMES Group 1: Xiangling Liu.
TIC-TAC-TOE FELIX CHEN CLUSTER 5: Computers in Biophysics and Robotics.
Presented by : Ashin Ara Bithi Roll : 09 Iffat Ara Roll : 22 12th Batch Department of Computer Science & Engineering University of Dhaka.
1 Introduction to Computing Lecture 15 Arrays (Part 1) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo.
Loops For loop for n = [ ] code end While loop while a ~= 3 code end.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Return function Random function Recursion function Function in C 1.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Artificial Intelligence and Robotics By Keith Bright & John DeBovis.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Computer Programming for Engineers
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Engineering Problem#1 Engr201 Computer Programming for Engineers Faculty of Engineering Chiang Mai University 1.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Arrays. Arrays are objects that help us organize large amounts of information.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Week 12 Class Activities.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
ECE Application Programming
Programming application CC213
Chapter 2 Overview of C.
CS1100 Computational Engineering
The while Looping Structure
EKT150 : Computer Programming
The while Looping Structure
Functions continued.
Tic Tac Toe application
CSCE 206 Lab Structured Programming in C
The while Looping Structure
EECE.2160 ECE Application Programming
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

Computer Programming for Engineers

Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between players

Tic-Tac-Toe, O-X: Flow of the game O O X X Starting with a clean 3x3 grid Taking turns Marking the spaces Checking for winners Drawing a new grid Until there is a winner

Drawing a 3x3 grid Input/Output? A 2 dimensional array stores O,X in the main Output? How to draw a grid? # of status of the var? 1 space for var 1 space for “|” 1 space for “_” 1 space for “+” An empty space

Drawing a 3x3 grid Q1: Write the following program Declare a 3x3 (2-D) array, Initialize members in the array to zero (0) Randomly generate members in the array to 0 – 2 Write a function that draws the grid and if the value is 1, draw O the value is 2, draw X the value is 0, draw a space Call the above function

q1.c #include void print_tbl(int [3][3]); void main(void) { int table[3][3]={0,0,0,0,0,0,0,0,0}; int i, j; for (i=0; i<3; i++) for(j=0; j<3; j++) table[i][j] = rand()%3; print_tbl(table); } void print_tbl(int tbl[3][3]) { int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf(" "); if (tbl[i][j]==1) printf("o"); else if (tbl[i][j]==2) printf("x"); else printf(" "); if (j!=2) printf("|"); } printf("\n"); if (i!=2) printf(" \n"); }

Receiving the inputs The inputs are the 2-D coordinate as follows Receiving the row #, then column # 2,02,1 1,01,1 0,00,1 2,2 1,2 0,2

Receiving the inputs: Cont. Q2: Add codes to Q1 to accept the location for the ‘O’ player as follows: Enter O horizontal axis: 1 Enter O vertical axis: 0 If the location is occupied, re-prompt for another location. O O X X O

q2.c #include void print_tbl(int [3][3]); void main(void) { int table[3][3]={0,0,0,0,0,0,0,0,0}; int i, j; int h, v; for (i=0; i<3; i++) for(j=0; j<3; j++) table[i][j] = rand()%3; print_tbl(table); do { printf("Enter o horizontal axis: "); scanf("%d", &h); printf("Enter o vertical axis: "); scanf("%d", &v); }while(table[h][v]==1||table[h][v]==2); table[h][v]=1; print_tbl(table); } void print_tbl(int tbl[3][3]) { int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf(" "); if (tbl[i][j]==1) printf("o"); else if (tbl[i][j]==2) printf("x"); else printf(" "); if (j!=2) printf("|"); } printf("\n"); if (i!=2) printf(" \n"); }

Checking for a winner O O X X O O XX O X A winner is found, when a player place their marks three in a row, which could be horizontal, vertical and diagonal

Checking for a winner (Cont.) Q3: Write the following program Declare a 3x3 array Initialize all members to zero Checking for winner on row#0, and Return 0, if no winner Return 1, if ‘0’ won Return 2, if ‘x’ won Test the program with random numbers

q3.c // unused codes can be marked as comments #include void print_tbl(int [3][3]); int check_winning(int [3][3]); void main(void) { int table[3][3]={0,0,0,0,0,0,0,0,0}; int winner = 0; table[0][0]=1; table[0][1]=1; table[0][2]=1; print_tbl(table); while (winner==0) { winner = check_winning(table); } if (winner==1) printf("O win\n"); else if (winner==2) printf("X win\n"); } int check_winning(int tbl[3][3]) { if (tbl[0][0]==1 &&tbl[0][1]==1 &&tbl[0][2]==1) return 1; else if (tbl[0][0]==2 &&tbl[0][1]==2 &&tbl[0][2]==2) return 2; else return 0; }

Players taking turns Create a variable storing the status of current player 0 for player ‘o’ 1 for player ‘x’ Toggling between players can be done by using the logic: !0 -> 1 !1 -> 0

Players taking turns (Cont.) Q4: Modify codes for Q2 and Q3 such that: Create empty spaces on a 3x3 grid Starting with player ‘o’ and taking turns Input the location for ‘0’ and ‘x’ where it is not occupied Check for a winner for row#0

q4.c #include void print_tbl(int [3][3]); int check_winning(int [3][3]); void main(void) { int table[3][3]={0,0,0,0,0,0,0,0,0}; int h, v, winner = 0, player=0; print_tbl(table); while (winner==0) { if (player==0) { do { printf("Enter o horizontal axis: "); scanf("%d", &h); printf("Enter o vertical axis: "); scanf("%d", &v); } while(table[h][v]==1||table[h][v]==2); table[h][v]=1; } else { do { printf("Enter x horizontal axis: "); scanf("%d", &h); printf("Enter x vertical axis: "); scanf("%d", &v); } while ( (table[h][v]==1||table[h][v]==2); table[h][v]=2; } winner = check_winning(table); player=!player; printf("\n"); print_tbl(table); } if (winner==1) printf("O win!\n"); else if (winner==2) printf("X win!\n"); }

Note that the code on the left can be rewritten as the one of the right if (player==1) { do { printf("Enter o row: "); scanf("%d", &h); printf("Enter o column: "); scanf("%d", &v); } while(table[h][v]==1||table[h][v]==2); table[h][v]=1; } else { do { printf("Enter x row: "); scanf("%d", &h); printf("Enter x column: "); scanf("%d", &v); } while ( (table[h][v]==1||table[h][v]==2); table[h][v]=2; } char p;. if (player==1) p = ‘o’; else p = ‘x’; do { printf("Enter %c row: “, p); scanf("%d", &h); printf("Enter %c column: “, p); scanf("%d", &v); } while(table[h][v]==1||table[h][v]==2); table[h][v]=player;

Lab Sheet The previous program does not consider the draw case. Modify the code so that it can deal with the draw case.