Пример #1
0
Файл: flip.c Проект: CPonty/Flip
void parse_setup (int argc, char * argv[], gameType * game) {
    /*
     Process arguments from main() for starting a game
     */
    int a, b = 0, c = 0, i;
	
	/* Strip non-integer characters after the last argument */
	if (argc == 4) {
		string_strip_nondigit(argv[3]);
	} else if (argc == 5) {
		string_strip_nondigit(argv[4]);
	}
	/* Check arguments are positive integers */
    for (i = 2; i < argc; i++) {
        if (!string_is_numeric(argv[i])) {
            if (i == 2) {
                sysMessage(5, game);
            } else {
                sysMessage(6, game);
            }
        }
    } 
    /* get parameters */
	if (argc >= 4) {
		b = atoi(argv[3]);
		if (argc == 5) {
			c = atoi(argv[4]);
		}
	}
	a = atoi(argv[2]);
    if (a <= 3) {
        /* Boardsize is invalid */
        sysMessage(5, game);
        
    } else if ( (b > 2) || (c > 2) || (b < 0) || (c < 0) ) {
        /* Player selection is invalid */
        sysMessage(6, game);
        
    } else {
        /* Valid game start condition! */
        board_ini(&game->board, a);
        board_ini(&game->validMove, a);
        game->pTypeX = b;
        game->pTypeO = c;
        play(game);
    }
}
Пример #2
0
Файл: flip.c Проект: CPonty/Flip
void input_turn(gameType * game) {
    /*
        Read and split player input into arguments for the parser
    */
    int pos;
    int bufUsed = 0, bufSize = BUFFER_INCREMENT;
    char * buf, * arg1, * arg2, * temp, c;
    
    printf("Player (%c)> ", game->whoseTurn);
    
    /* Read stdin to buffer */
    buf = (char *) malloc(sizeof(char) * bufSize);
    while ( (c = getchar()) != '\n' && !feof(stdin) ) {
        bufUsed++;
        /* Expand memory if needed */
        if (bufSize <= bufUsed) {
            bufSize += BUFFER_INCREMENT;
            temp = (char *) malloc(sizeof(char) * bufSize);
            memcpy(temp, buf, sizeof(char) * bufSize-BUFFER_INCREMENT);
            buf = temp;
			free(temp);
        }
        buf[bufUsed-1] = c;
    }
    buf[bufUsed] = '\0';
    /* Catch EoF-only input */
    if ((bufUsed == 0) && (c != '\n')) {
        sysMessage(10, game);
    }
    
	if (buf[0]=='s') {
		
		/* Save file */
		parse_turn(1, buf, "", game);
		
	} else {
		
		/* Play a turn */
		pos = string_find(buf, ' ');
		if (pos > 0) {
			/* Two arguments */
			arg1 = (char *) malloc( sizeof(char) * (pos+1) );
			arg2 = (char *) malloc( sizeof(char) * (bufUsed-pos) );
			memcpy( arg1, &buf[0], sizeof(char) * pos);
			memcpy( arg2, &buf[pos+1], sizeof(char) * (bufUsed-pos-1));
			arg1[pos] = '\0';
			arg2[strlen(buf)-pos-1] = '\0';
			/* Send to parser and free memory */
			parse_turn(2, arg1, arg2, game);
			free(arg1);
			free(arg2);
		}
	}
	free(buf);
}
Пример #3
0
Файл: flip.c Проект: CPonty/Flip
void game_save (char * fname, gameType * game) {
/*
    Try to write state-dependent parts of '*game' to file
 
    'flip'
    passes
    pType1
    pType2
    whoseTurn
    board.n
    board.s[0]
    ...
    board.s[n-1]
*/
    FILE * f;
    int i;
    char * validator = PROG_NAME;
    game_set_fname(fname, game);
    
    /* Check file writable */
    if (strlen(fname) == 0) {
        sysMessage(9, game);
        return;
    }
    f = fopen(fname, "w");
    if (f == NULL) {
        sysMessage(8, game);
        return;
    }
    /*Write validation code, variables & board array*/
    fwrite(validator, 1, sizeof(char) * strlen(PROG_NAME), f);
    fwrite(&game->passes, 1, sizeof(int), f);
    fwrite(&game->pTypeO, 1, sizeof(int), f);
    fwrite(&game->pTypeX, 1, sizeof(int), f);
    fwrite(&game->whoseTurn, 1, sizeof(char), f);
    fwrite(&(game->board).n, 1, sizeof(int), f);
    for (i = 0; i < (game->board).n; i++) {
        fwrite((game->board).s[i], 1, sizeof(char) * (game->board).n, f);
    }
    fclose(f);
    sysMessage(4, game);
}
Пример #4
0
Файл: flip.c Проект: CPonty/Flip
void turn_decision (gameType * game) {
    /*
        Make a decision on what to do in a turn, based on game state
    */
    char player;
    
    /* Refresh background information */
    player = game->whoseTurn;
    game_update_valid_moves(game);
    game_update_scoring(game);
    
    /* Board is full: end the game */
    if (board_missing_char('.', &game->board)) {
        sysMessage(2, game);
    }
    
    /* Player has no move options: pass */
    else if (board_missing_char(player, &game->validMove)) {

        printf("%c passes.\n", game->whoseTurn);
        game_next_player(game);
        (game->passes)++;
        
        /* Both players passed: end the game */
        if (game->passes > 1) {
            sysMessage(3, game);
        }
        return;
    }
    
    /* AI player: place a tile */
    else if ((player == 'O') && ((game->pTypeO) != 0)) {
        ai_turn(game->pTypeO, game);
    } else if ((player == 'X') && ((game->pTypeX) != 0)) {
        ai_turn(game->pTypeX, game);
    }
    
    /* Human player: input prompt */
    else {
        input_turn(game);
    }   
}
Пример #5
0
Файл: flip.c Проект: CPonty/Flip
void game_load (char * fname, gameType * game) {
    /*
        Try to load & apply a gamestate from the file
            Assumes gameplay has not yet started
    */
    FILE * f;
    int i;
    char validator[5] = {'\0'};
    game_set_fname(fname, game);
    
    /* Check file readable */
    if (strlen(fname) == 0) {
        sysMessage(7, game); 
    }
    f = fopen(fname, "r");
    if (f == NULL) {
        sysMessage(7, game);
    }
    /* Simple 'file-is-a-flip-save-file' check */
    fread(validator, 1, sizeof(char) * strlen(PROG_NAME), f);
    if ((strcmp(validator, PROG_NAME))) {
        sysMessage(7, game);
    }
    
    /* Read new data */
    fread(&game->passes, 1, sizeof(int), f);
    fread(&game->pTypeO, 1, sizeof(int), f);
    fread(&game->pTypeX, 1, sizeof(int), f);
    fread(&game->whoseTurn, 1, sizeof(char), f);
    fread(&(game->board).n, 1, sizeof(int), f);
    /* board/validMove state */
    (game->validMove).n = (game->board).n;
    board_ini(&game->board, (game->board).n);
    board_ini(&game->validMove, (game->validMove).n);
    for (i = 0; i < (game->board).n; i++) {
        fread((game->board).s[i], 1, sizeof(char) * (game->board).n, f);
    }
    fclose(f);
}
Пример #6
0
Файл: flip.c Проект: CPonty/Flip
void parse_ini (int argc, char * argv[], gameType * game) {
    /* 
     Decide on action to take on startup
     */
    if (argc == 1) {
        /* No parameters */
        sysMessage(1, game);
        
    } else if (!strcmp(argv[1], "new") && (argc > 2) && (argc <= 5)) {
        /* Start new game */
        parse_setup(argc, argv, game);
        
    } else if (!strcmp(argv[1], "load") && (argc == 3)) {
        /* Load a game */
        game_load(argv[2], game);
        play(game);
        
    } else {
        /* Wrong parameters */
        sysMessage(11, game);
    }
}
Пример #7
0
Файл: flip.c Проект: CPonty/Flip
void ai_turn (int playerType, gameType * game) {
    /*
        Parse through the valid moves using one of the AI types
     */
    int x, y, dy, size;
    size = (game->validMove).n;
    
    /* Choose an AI search pattern */
    if (playerType == 1) {
        x = 0;
        y = 0;
        dy = 1;
    } else {
        x = size-1;
        y = size-1;
        dy = -1;
    }
    
    /* Find an available position */
    while (1) {
        /* is this position a valid move? */
        if ((game->validMove).s[x][y] == game->whoseTurn) {
            break;
        }
        y += dy;
        /* walked off ends of board - wrap around */
        if ((y < 0)) {
            y = size-1;
            x--;
        } else if (y >= size) {
            y = 0;
            x++;
        }
        /* walked off top/bottom of board - suspend; debug problem */
        if ((x < 0) || (x >= size)) {
            sysMessage(0, game);
        }
    }
    /* Place tile, display & prepare for the next player */
    game_put_tile(x, y, game);
    printf("Player %c moves at %d %d.\n", game->whoseTurn, x, y);
    board_print(&game->board);
    game_next_player(game);
    game->passes = 0;
}
Пример #8
0
/*! \internal */
MessageControl::MessageControl() :
    phoneValueSpace("/Communications/Messages"),
    smsMemFull("/Telephony/Status/SMSMemoryFull"),
#ifdef QTOPIA_CELL
    smsreq(0),
#endif
    messageCountUpdate(0),
    channel("QPE/System"),
    smsCount(0), mmsCount(0), systemCount(0),
    smsIsFull(false), prevSmsMemoryFull(0)
{
    QSettings setting("Trolltech", "qpe");
    setting.beginGroup("Messages");
    smsCount = setting.value("MissedSMSMessages", 0).toInt();
    mmsCount = setting.value("MissedMMSMessages", 0).toInt();
    systemCount = setting.value("MissedSystemMessages", 0).toInt();

    mgr = new QCommServiceManager( this );
    connect( mgr, SIGNAL(servicesChanged()), this,
             SLOT(telephonyServicesChanged()) );
    telephonyServicesChanged(); // Check to see if we already have SMS.

    connect(&channel, SIGNAL(received(QString,QByteArray)),
            this, SLOT(sysMessage(QString,QByteArray)) );

    connect(&smsMemFull, SIGNAL(contentsChanged()),
            this, SLOT(smsMemoryFullChanged()) );
    prevSmsMemoryFull = smsMemFull.value().toInt();

    // React to changes in incoming message count
    messageCountUpdate = new QtopiaIpcAdaptor("QPE/Messages/MessageCountUpdated");
    QtopiaIpcAdaptor::connect(messageCountUpdate, MESSAGE(changeValue()), 
                              this, SLOT(messageCountChanged()));

    doNewCount(false);
    messageCountChanged();
}
Пример #9
0
LightSettings::LightSettings( QWidget* parent,  Qt::WFlags fl )
    : QDialog( parent, fl), isStatusView( false )
{
    setWindowTitle(tr("Power Management"));
    QVBoxLayout * baseLayout = new QVBoxLayout( this );
    baseLayout->setMargin( 0 );

    QWidget * container = new QWidget();

    QScrollArea *sView = new QScrollArea;
    sView->setFocusPolicy(Qt::NoFocus);
    sView->setFrameStyle(QFrame::NoFrame);
    sView->setWidget( container );
    sView->setWidgetResizable( true );
    sView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QVBoxLayout *lightLayout = new QVBoxLayout(container);
    lightLayout->setMargin( 0 );
    b = new LightSettingsContainer();
    QtopiaApplication::setInputMethodHint( b->interval_dim, QtopiaApplication::AlwaysOff );
    QtopiaApplication::setInputMethodHint( b->interval_lightoff, QtopiaApplication::AlwaysOff );
    QtopiaApplication::setInputMethodHint( b->interval_suspend, QtopiaApplication::AlwaysOff );

    lightLayout->addWidget(b);

    baseLayout->addWidget(sView);

    // add context menu to push its status to Profiles
    contextMenu = QSoftMenuBar::menuFor( this );
    QAction* actionCapture = new QAction( QIcon( ":icon/Note" ), tr( "Add to current profile" ), this );
    contextMenu->addAction( actionCapture );
    connect( actionCapture, SIGNAL(triggered()), this, SLOT(pushSettingStatus()) );
    connect( qApp, SIGNAL(appMessage(QString,QByteArray)),
        this, SLOT(receive(QString,QByteArray)) );

    connect( b->interval_dim, SIGNAL(valueChanged(int)), this, SLOT(updateLightOffMinValue(int)) );
    connect( b->interval_dim, SIGNAL(valueChanged(int)), this, SLOT(updateSuspendMinValue(int)) );
    connect( b->interval_lightoff, SIGNAL(valueChanged(int)), this, SLOT(updateSuspendMinValue(int)) );

    b->officon->setPixmap(QPixmap(":image/off").scaled(24, 24, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
    b->brighticon->setPixmap(QPixmap(":image/Light").scaled(24, 24, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));

    QSettings hwConfig("Trolltech", "Hardware");
    hwConfig.beginGroup("PowerManagement");
    lockMode.canSuspend = hwConfig.value("CanSuspendLock", false).toBool();    
    batteryMode.canSuspend = hwConfig.value("CanSuspend", false).toBool();
    externalMode.canSuspend = hwConfig.value("CanSuspendAC", false).toBool();
    hwConfig.endGroup();

    b->notnetworkedsuspend->hide();

    if (batteryMode.canSuspend || externalMode.canSuspend || lockMode.canSuspend) {
        b->interval_suspend->setEnabled(true);
    } else {
        b->interval_suspend->hide();
        b->label_suspend->hide();
    }

    QSettings config("Trolltech","qpe");
    
    config.beginGroup("LockPower");
    lockMode.intervalDim = config.value( "Interval_Dim", 20 ).toInt();
    lockMode.intervalLightOff = config.value("Interval_LightOff", 30).toInt();
    lockMode.intervalSuspend = config.value("Interval", 60).toInt();
    lockMode.brightness = config.value("Brightness", 255).toInt();
    lockMode.brightness = qMax(1,lockMode.brightness * qpe_sysBrightnessSteps() / 255);
    lockMode.initBrightness = lockMode.brightness;
    lockMode.dim = config.value("Dim", true).toBool();
    lockMode.lightoff = config.value("LightOff", false).toBool();
    lockMode.suspend = config.value("Suspend", true).toBool();
    lockMode.networkedsuspend = config.value("NetworkedSuspend", true).toBool();
    config.endGroup();

    config.beginGroup("BatteryPower");
    batteryMode.intervalDim = config.value( "Interval_Dim", 20 ).toInt();
    batteryMode.intervalLightOff = config.value("Interval_LightOff", 30).toInt();
    batteryMode.intervalSuspend = config.value("Interval", 60).toInt();
    batteryMode.brightness = config.value("Brightness", 255).toInt();
    batteryMode.brightness = qMax(1,batteryMode.brightness * qpe_sysBrightnessSteps() / 255);
    batteryMode.initBrightness = batteryMode.brightness;
    batteryMode.dim = config.value("Dim", true).toBool();
    batteryMode.lightoff = config.value("LightOff", false).toBool();
    batteryMode.suspend = config.value("Suspend", true).toBool();
    batteryMode.networkedsuspend = config.value("NetworkedSuspend", true).toBool();
    config.endGroup();

    config.beginGroup("ExternalPower");
    externalMode.intervalDim = config.value( "Interval_Dim", 20 ).toInt();
    externalMode.intervalLightOff = config.value("Interval_LightOff", 30).toInt();
    externalMode.intervalSuspend = config.value("Interval", 240).toInt();
    externalMode.brightness = config.value("Brightness", 255).toInt();
    externalMode.brightness = qMax(1,externalMode.brightness * qpe_sysBrightnessSteps() / 255);
    externalMode.initBrightness = externalMode.brightness;
    externalMode.dim = config.value("Dim", true).toBool();
    externalMode.lightoff = config.value("LightOff", false).toBool();   //default to leave on
    externalMode.suspend = config.value("Suspend", true).toBool();
    externalMode.networkedsuspend = config.value("NetworkedSuspend",false).toBool();
    config.endGroup();

    //must set min > 0 the screen will become completely black
    int maxbright = qpe_sysBrightnessSteps();
    b->brightness->setMaximum( maxbright );
    b->brightness->setMinimum( 1 );
    b->brightness->setTickInterval( qMax(1,maxbright/16) );
    b->brightness->setSingleStep( qMax(1,maxbright/16) );
    b->brightness->setPageStep( qMax(1,maxbright/16) );

    currentMode = &batteryMode;
    applyMode();

    connect(b->powerSource, SIGNAL(currentIndexChanged(int)),
            this, SLOT(powerTypeChanged(int)));
    if ( powerStatus.wallStatus() == QPowerStatus::Available ) {
        b->powerSource->setCurrentIndex(1);
    }

    connect(b->brightness, SIGNAL(valueChanged(int)), this, SLOT(applyBrightness()));

    QtopiaChannel *channel = new QtopiaChannel("Qtopia/PowerStatus", this);
    connect(channel, SIGNAL(received(QString,QByteArray)),
            this, SLOT(sysMessage(QString,QByteArray)));
}