ABXZone Computer  Forums



Welcome to the ABXZone Computer Forums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Reply
 
LinkBack Thread Tools Display Modes
Old 04-04-2009, 09:38 AM   #1
Registered User
 
Tbolt's Avatar
 
Join Date: Jul 2006
Posts: 5
Nested looping in C - Im stuck

So I thought I had a pretty good understanding of looping even though I missed class the day it was taught. However now I am in a conceptual mind**** of how to approach this.

The assignment is to create a dynamic tic-tac-toe board using multidimensional arrays and functions (with array passing)

here is what the output should look like:
hxxp://i4.photobucket.com/albums/y108/phishkids11/toe1.jpg


here is what I have so far, focusing on the display board part
Code:
// INCLUDES
#include <stdio.h>

// DEFINES
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif

// ROWS and COLS
#define ROWS 7
#define COLS 7

#define MARKONE 'X'
#define MARKTWO 'O'
#define BLANK   ' '

// PROTOTYPES
void InitializeBoard(char[ROWS][COLS]);
void DisplayBoard(char[ROWS][COLS]);
int PlayerMove(int, int, char[ROWS][COLS], char);


// MAIN
int main() {
	// declare variables
	char board[ROWS][COLS];

	// initialize board
	InitializeBoard(board);
/*
	// populate the board with moves
	PlayerMove(1, 1, board, MARKONE);
	PlayerMove(1, 2, board, MARKONE);
	PlayerMove(4, 3, board, MARKONE);
	PlayerMove(1, 1, board, MARKTWO);
	PlayerMove(6, 2, board, MARKTWO);
	PlayerMove(4, 12, board, MARKTWO);
*/
	// display the board
	DisplayBoard(board);
	
	// exit program
	return 0;
}

// FUNCTION IMPLEMENTATIONS
void InitializeBoard(char board[ROWS][COLS]) {
	int i = 0, j = 0;
	for(i = 0; i < ROWS; i++) {
		for(j = 0; j < COLS; j++) {
			board[i][j] = BLANK;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS]) {
	int i = 0, j = 0, k = 0;
	printf("**************TIC-TAC-TOE**************\n\n");

	for(i = 1; i <= COLS; i++) {
		printf("%2d", i);
	}
		printf("\n\n");    
		
        

}
			



/*
int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {
	
}
*/
I feel like I am trying to approach this from the wrong angle, and because of that I can't comprehend how to do it. I imagine I will need to nest a loop within a loop within another loop.

Any advice would be great, not necessarily with this particular problem but anything with looping that would help me understand it better would be great.
(Offline)   Reply With Quote

 
Old 04-04-2009, 03:41 PM   #2
XJ.
Back at the Zone!
 
XJ.'s Avatar
 
Join Date: Apr 2002
Location: NH, USA
Posts: 6,463
Re: Nested looping in C - Im stuck

For your DisplayBoard() function, just do it the same way you did the InitializeBoard() function. Just replace 'board[i][j] = BLANK' with the appropriate print function.

Good luck!
__________________

Using my PC for work ATM, so no folding for now.
(Offline)   Reply With Quote
Old 04-04-2009, 05:48 PM   #3
Registered User
 
Tbolt's Avatar
 
Join Date: Jul 2006
Posts: 5
Re: Nested looping in C - Im stuck

Alright, I am starting to get it. I just had to go back to the basics and play around a bit before tackling this.

Thanks though =p
(Offline)   Reply With Quote
Old 04-04-2009, 08:03 PM   #4
&lt;unknown level&gt;
 
zapionics<alt>'s Avatar
 
Join Date: Dec 2002
Location: AUSTRALIA
Posts: 3,880
Re: Nested looping in C - Im stuck

just on this "developers block" syndrome you seemed to be experiencing... I'm a big fan of design... ie: conceptualising the solution via a process of design which then gives you a skeletal structure in which to hang your code.

I tend to use "structure" charts for this... and use cases of course... and activity diagrams or even sequence diagrams...

I know this may be a trivial example for some ppl... coding top down has advantages (eg: speed) if you are very familiar and experienced with the solution domain... but otherwise... design!
__________________
*** Please note: any advertising within this post has been placed there by the site owner and NOT by me! ***


"To Boldly Go..."
(Offline)   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




Powered by vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.1
vBulletin Skin developed by: vBStyles.com

© 2006 - 2009 ABXZone Forums | About ABX Zone Forums | Advertising Opportunities | Legal | A member of the Crowdgather Forum Community